A simple production-ready backend server template for Content Management Systems build with Rust and Axum.
- Authors (Authors of posts)
- Posts (The actual content to be managed)
- EditSuggestion (WIP)
- Google sign-up/sign-in (Coming soon)
- Database used: Postgres
- Cache used: Redis
-
POST
/signup
: Sign up with name, email, and password -
POST
/login
: Login with email and password -
GET
/author
: get all authors -
GET
/author/:id
: Get specific author. -
PATCH
/author/:id
: Edit specific author. -
GET
/post
: Get all posts -
GET
/post/:id
: Get specific post -
PATCH
/post/:id
: Edit specific post -
DELETE
/post/:id
: Delete specific post
- Create a .env file in your local copy of the repository, and set the following env variables
PROD_DATABASE_URL
: Your postgres database for productionDEV_DATABASE_URL
: Your postgres database for developmentJWT_SECRET
: Your JWT secretDEV_REDIS_CONN_URL
: Your redis connection url for development, set toPROD_REDIS_CONN_URL
: Your redis connection url for productionMODE
: Set as "production"(CASE SENSITIVE!) to enable production mode (setting this absent or as any other value will run the program in development mode)DOCKER_IMAGE_NAME
: The name of your docker image
- With all your environment variables set, start up your postgres dev server.
# Default config
# Start postgresql server docker image:
docker run --rm --name pg -p 5433:5433 \
-e POSTGRES_PASSWORD=welcome \
postgres:15
# (optional) To have a psql terminal on pg.
# In another terminal (tab) run psql:
docker exec -it -u postgres pg psql
# (optional) For pg to print all sql statements.
# In psql command line started above.
ALTER DATABASE postgres SET log_statement = 'all';
- Run
cargo run
in your terminal to compile and run your project. - To deploy to docker, run the deploy script with the command
./deploy.sh
in the project directory
- IMPORTANT!: If you decide to change
DEV_DATABASE_URL
, edit the following files accordingly:sql\dev_initial\00-recreate-db.sql
src\_dev_utils\dev_db.rs
- Use the "WithRejection<
CUSTOM_JSON_BODY
, ApiError>" as Json body type in order to enable JSON extraction errors - All errors can be found in
src/models/error.rs
in theError
enum. You may write custom responses for each error inside theimpl IntoResponse
block for theError
enum - Tests are stored in:
/examples
folder - Run the tests with the command:
cargo run --example {FILE_NAME}
, whereFILE_NAME
is the name of the file containing the test - With
cargo watch
installed, Re-run the test on each file save with the command:cargo watch -q -c -w examples/ -x "run --example {FILE_NAME}"
, whereFILE_NAME
is the name of the file containing the test