How thanipro/axum-rust-rest-api-template works
Project type and purpose
• This is a template for a RESTful API service written in Rust, built on the Axum web framework with SQLx as the database access layer.
• Its purpose is to provide a clean, SOA-style directory structure and boilerplate for building register, login (token issuance), and profile endpoints on top of a MySQL (or configurable) database.
File structure
• Top-level:
• .env / .env.example for environment configuration
• migrations/ for SQL migration scripts
• Cargo.toml/Cargo.lock for Rust dependencies
• src/main.rs to bootstrap the server
• src/ subdivides by layer:
• config/ – database initialization and parameter loading
• entity/ – domain entities mapping to database tables
• error/ – custom error types per layer
• handler/ – HTTP handlers (core of each route)
• middleware/ – request interceptors (e.g. JWT auth)
• repository/ – data access methods using SQLx
• response/ – unified API response wrapper
• routes/ – route definitions grouping paths to handlers
• service/ – business logic (token management, user operations)
• state/ – shared state (database pool, auth/token state)
README for additional insights
• Uses Axum and SQLx, follows a SOA pattern rather than monolithic main.rs
• Three basic endpoints: register, login, profile
• Database is MySQL by default; easily swapped out via config/database.rs and migrations.
a. Main components
• Clients (Postman, web app, mobile apps)
• HTTP server (Axum) with a router
• Middleware layer (authentication)
• Handlers (register_handler, auth_handler, profile_handler)
• Service layer (user_service, token_service)
• Repository layer (user_repository)
• Database (MySQL via SQLx connection pool)
• Config & State modules (parameter loader, database pool initializer)
• Migrations folder (external tool for schema setup)
b. Relationships and interactions
1. Client → HTTP request → Axum Router
2. Router dispatches to appropriate Handler
3. Handler invokes Middleware (e.g., validate JWT) if required
4. Handler calls into Service layer for business logic
5. Service uses Repository to read/write to Database
6. Repository executes SQLx queries via the shared Pool
7. Data flows back up: Repository → Service → Handler → Response to Client
c. Architectural patterns and principles
• Separation of Concerns: clear layering (API → Handler → Service → Repository → DB)
• Dependency Injection via shared State (database pool, auth/token state)
• DTOs isolate external API contracts from Entity models
• Centralized Error types and Response envelopes for consistent error handling
d. Key technologies and libraries
• Rust & Tokio for async runtime
• Axum for HTTP server and routing
• SQLx for compile-time-checked SQL queries and connection pooling
• dotenv or custom parameter loader for environment config
• async-trait for abstracting database backends
Detail
• Initialization sequence is visible: loading .env → building Config → establishing DB pool → launching Axum server with attached state.