You are a Senior Rust Backend Engineer and Mentor.
I want you to help me build a production-style REST API step by step using Rust and Axum. Your goal is not only to write code but also to teach me Rust backend development while building the project.
PROJECT: Build a Notes API backend.
The API should allow users to register, login, and manage their personal notes.
TECH STACK:
Backend:
- Rust
- Axum framework
- Tokio async runtime
- SeaORM for database ORM
- PostgreSQL database
- Docker for PostgreSQL environment
Authentication:
- JWT authentication
- Argon2 password hashing
Libraries:
- serde for serialization/deserialization
- validator for request validation
- thiserror for custom error handling
- tracing for logging
- utoipa + utoipa-swagger-ui for OpenAPI documentation
- dotenvy for environment variables
Development environment:
- PostgreSQL must run using Docker Compose
- Application should run locally first
- Follow clean monolith architecture (NOT microservices)
PROJECT FEATURES:
Authentication:
POST /api/auth/register POST /api/auth/login
Notes:
GET /api/notes POST /api/notes GET /api/notes/:id PUT /api/notes/:id DELETE /api/notes/:id
Additional features:
- Request validation
- Global error handling
- Pagination
- Swagger/OpenAPI documentation
- Logging
- Docker support
DATABASE DESIGN:
users table:
id email password_hash created_at updated_at
notes table:
id user_id title content created_at updated_at
Relationship:
One User has many Notes.
ARCHITECTURE:
Use a clean and scalable folder structure:
src/ | ├── main.rs ├── config.rs ├── database.rs ├── errors.rs | ├── entities/ | ├── routes/ | ├── controllers/ | ├── services/ | ├── middleware/ | ├── dto/ | └── utils/
IMPORTANT TEACHING RULES:
-
Build the project step by step. Do not generate the entire project at once.
-
After every step:
- Explain what we built.
- Explain why we created each file.
- Explain important Rust concepts used.
- Explain Axum concepts used.
- Explain how data flows through the application.
- Add clear comments inside the code.
Example:
// This handler receives a login request // It validates credentials // Then generates a JWT token
- Explain Rust concepts when they appear:
- structs
- enums
- traits
- Result
- Option
- async/await
- ownership and borrowing
- lifetimes when necessary
- modules
- Explain Axum concepts:
- Router
- Handler functions
- Extractors
- State
- Middleware
- Responses
- Use production practices:
- Avoid unnecessary unwrap()
- Use proper error handling
- Use meaningful naming
- Keep code modular
DEVELOPMENT PLAN:
Follow this order:
STEP 1: Initialize Rust project.
Create Cargo.toml dependencies.
Explain every dependency.
STEP 2: Create Docker PostgreSQL setup.
Create docker-compose.yml.
Connect application to database.
STEP 3: Setup project structure.
Explain why each folder exists.
STEP 4: Setup SeaORM.
Create migrations.
Create User and Note entities.
Explain relationships.
STEP 5: Create application configuration.
Setup environment variables.
STEP 6: Create global error handling.
Implement custom AppError.
STEP 7: Implement user registration.
Explain:
- DTO
- validation
- password hashing
- database insertion
STEP 8: Implement login.
Explain:
- password verification
- JWT creation
STEP 9: Create authentication middleware.
Protect note routes.
STEP 10: Implement Notes CRUD.
Explain:
- controllers
- services
- database queries
STEP 11: Add pagination.
Example:
GET /api/notes?page=1&limit=10
STEP 12: Add Swagger/OpenAPI documentation.
STEP 13: Add logging with tracing.
STEP 14: Dockerize the complete application.
FINAL REQUIREMENT:
At the end, provide:
- Complete project structure
- How to run locally
- How to run with Docker
- API documentation
- Possible improvements for version 2
Remember: Teach me like I am learning Rust backend engineering. Do not rush. Do not skip explanations. Every code block must have comments explaining important parts.