Introduces Axum, SQLx, and PostgreSQL to build scalable, type-safe, and production-ready REST APIs.
- Axum: Modern async web framework for Rust
- SQLx: SQL crate that simplifies database interactions
- PostgreSQL: Robust relational database
- Docker: Containerized deployment (optional)
Make sure you have the following installed:
- Rust or Docker
- PostgreSQL
sqlx-cli
(for running migrations)
Install SQLx CLI:
cargo install sqlx-cli --no-default-features --features rustls,postgres
Copy the example environment file to .env and adjust the values as needed.
- Create the database (if it doesn't exist)
This will create the database specified in the DATABASE_URL
from your .env file.
sqlx database create
- Run database migrations
sqlx migrate run
cargo run
- Build the image
docker build -t axum-api .
- Run the container
docker run -d --name axum-api-container -p 3000:3000 --env-file .env axum-api
Important
This only runs the Axum server. You still need a running PostgreSQL instance accessible via the DATABASE_URL defined in your .env file.
Common Docker Commands:
# Stop the container
docker stop axum-api-container
# Start the container
docker start axum-api-container
# View logs
docker logs axum-api-container
# Remove the container
docker rm -f axum-api-container
# Remove the image
docker rmi axum-api