A production-ready template for building high-performance web APIs with Rust, Axum, and SeaORM.
- High-performance Web Server: Built on Axum, a fast and modular web framework
- Type-safe ORM: SeaORM for database operations with compile-time guarantees
- PostgreSQL Support: Robust relational database integration
- JWT Authentication: Secure authentication system out of the box
- RESTful API: Well-structured endpoints following best practices
- OpenAPI Documentation: Auto-generated API docs with Swagger UI
- Modular Architecture: Clean separation of concerns for maintainability
- Environment-based Configuration: Flexible configuration management
- Database Connection Pooling: Efficient connection handling
- Async/Await: Fully asynchronous for maximum performance
- Error Handling: Consistent error responses and logging
- Rust (latest stable version recommended)
- PostgreSQL (12+)
- Cargo (Rust's package manager)
- Docker and Docker Compose (optional, for containerized development)
-
Clone the repository:
git clone https://github.com/yourusername/axum-seaorm-postgresql-template.git cd axum-seaorm-postgresql-template
-
Set up environment variables:
cp .env.example .env
Update the
.env
file with your database credentials and other settings. -
Install dependencies and build:
cargo build
-
Run database migrations (requires SeaORM CLI):
cargo install sea-orm-cli sea-orm-cli migrate up
-
Start the server:
cargo run
-
Access the API documentation:
http://localhost:8000/docs
This project includes Docker configuration for easy development and deployment.
- Docker Engine 20.10.0+
- Docker Compose 2.0.0+
If you just want to build the Docker image without running it:
docker-compose build
This will build all services defined in the docker-compose.yml
file.
-
Build and start the application:
docker-compose up --build
-
For running in detached mode:
docker-compose up -d --build
-
View logs:
docker-compose logs -f
-
Stop the application:
docker-compose down
-
Stop and remove all containers, networks, and volumes:
docker-compose down -v
- app: The main application server (port 8000)
- db: PostgreSQL database (port 5432)
- migrate: Runs database migrations on startup
You can configure the application by creating a .env
file in the project root. See .env.example
for available options.
- The application will automatically reload when source code changes
- Database migrations run automatically on startup
- The database persists data in a Docker volume
src/
├── api/ # API routes and handlers
├── config/ # Application configuration
├── database/ # Database connection and setup
├── dto/ # Data Transfer Objects
├── entity/ # SeaORM entities
├── middleware/ # Axum middleware
├── service/ # Business logic
├── main.rs # Application entry point
└── state.rs # Application state
This project uses utoipa
to automatically generate OpenAPI documentation and serve it via Swagger UI.
- Swagger UI:
http://localhost:8000/docs
- OpenAPI JSON:
http://localhost:8000/api-doc/openapi.json
#[utoipa::path(
get,
path = "/v0/user/{id}",
params(
("id" = i32, Path, description = "User ID")
),
responses(
(status = 200, description = "Successfully retrieved user", body = UserInfoResponse),
(status = 404, description = "User not found"),
(status = 500, description = "Internal server error")
),
tag = "User"
)]
pub async fn get_user(
state: State<AppState>,
Path(id): Path<String>,
) -> Result<UserInfoResponse, Errors> {
// Handler implementation
}
This project follows a layered architecture:
- API Layer: Handles HTTP requests/responses
- Service Layer: Contains business logic
- Repository Layer: Manages database operations
- Domain Layer: Defines entities and DTOs
Configure the following in your .env
file:
DATABASE_URL=postgres://username:password@localhost:5432/dbname
JWT_SECRET=your_jwt_secret_key
PORT=8000
RUST_LOG=info
This project is licensed under the MIT License - see the LICENSE file for details.