How yuxuetr/axum-template works
Project type and purpose
• This is a Rust-based backend web service template built on the Axum framework.
• It exposes HTTP APIs, implements authentication (JWT with Ed25519), and provides user management via a Postgres database (using SQLx).
• The project uses a Nest.js–style organization: common utilities and individual business modules (auth, users), plus automated key generation, migrations, and REST-client fixtures.
File structure
• Top-level:
• .github/workflows/build.yml – CI pipeline.
• app.yaml – deployment config (e.g., App Engine).
• build.rs – build script that generates Ed25519 keys into fixtures.
• docs/, fixtures/, migrations/, rest_client/ – supporting artifacts.
• src/ – source code subdivided into:
– common/ – cross-cutting services (config loader, error definitions, auth helpers).
– modules/ – feature modules.
· auth/ – login, signup endpoints, JWT middleware.
· users/ – CRUD user management.
– lib.rs – root router and global state initialization (config + database pool).
– main.rs – server entry point.
README for additional insights
• Auth token uses Ed25519; keypairs auto-generated by build.rs.
• Global state bundles config + SQLx pool to be injected into service functions.
• Testing strategy: unit and integration tests in each module’s tests.rs.
• CI uses GitHub Actions; strict linting via pre-commit and cargo-deny.
a. Main components
1. API Gateway (Axum HTTP server)
2. Common Utilities (config loader, error handler, JWT helper)
3. Auth Module (handlers, services, middleware)
4. Users Module (handlers, services, entity definitions)
5. Global State (application state containing config & database connection pool)
6. Postgres Database
7. Build Script (build.rs) & Fixtures (keypair generation)
8. External CI/CD (GitHub Actions)
9. REST Client Tests (VS Code REST client)
b. Relationships & interactions
• Client → Axum HTTP Server (main.rs)
• HTTP Server routes → Module routers (registered in lib.rs)
• Handler functions call module Services (injected with &AppState)
• Services use SQLx via AppState.pool → Postgres
• Auth middleware validates JWT using public key from fixtures
• build.rs → generates keypair in fixtures before compile
• GitHub Actions → cargo build, cargo test, cargo deny
• REST client files hit HTTP endpoints as part of testing
c. Architectural patterns & principles
• Layered architecture: HTTP layer (handlers/middleware) → business logic (services) → data access (SQLx/entities).
• Modular organization: separate folders per business domain.
• Dependency injection via global AppState.
• Code generation (keys) at build time.
• Migration folder follows SQLx conventions.
d. Key technologies
• Rust, Axum, Tower Middleware
• SQLx + Postgres
• JWT-Ed25519 (via jwt-simple)
• GitHub Actions, pre-commit, cargo-deny
Key recommendations
• Migration folder as part of your “Database” component or as a deployment artifact.