Axum scaffold with clean architecture.
You probably don't need Rust on Rails.
Refer to this post for rationale and background.
- Axum framework
- SeaORM domain models
- Completely separated API routers and DB-related logic (named "persistence" layer)
- Completely separated input parameters, queries and output schemas
- OpenAPI documentation (Swagger UI and Scalar) powered by Utoipa
- Error handling with Anyhow
- Custom parameter validation with validator
- Optional Shuttle runtime
- Optional prefork workers for maximizing performance on Linux
api::routers
: Axum endpointsapi::error
: Models and traits for error handlingapi::extractor
Custom Axum extractorsapi::extractor::json
:Json
for bodies and responsesapi::extractor::valid
:Valid
for JSON body validation
api::validation
: JSON validation model based onvalidator
api::models
: Non domain model API modelsapi::models::response
: JSON error response
doc
: Utoipa doc declaration
Main concept: Web framework is replaceable.
All modules here should not include any specific API web framework logic.
app::persistence
: DB manipulation (CRUD) functionsapp::config
: DB or API server configurationapp::state
: APP state, e.g. DB connectionapp::error
: APP errors used byapi::error
. e.g. "User not found"
Main concept: Database (Sqlite/MySQL/PostgreSQL) is replaceable.
Except models::domains
and migration
, all modules are ORM library agnostic.
models::domains
: SeaORM domain modelsmodels::params
: Serde input parameters for creating/updating domain models in DBmodels::schemas
: Serde output schemas for combining different domain modelsmodels::queries
: Serde queries for filtering domain modelsmigration
: SeaORM migration files
tests::api
: API integration tests. Hierarchy is the same asapi::routers
tests::app::persistence
: DB/ORM-related unit tests. Hierarchy is the same asapp::persistence
utils
: Utility functionsmain
: Tokio and Shuttle conditional entry point
cp .env.example .env
# touch dev.db
# cargo install sea-orm-cli
# sea-orm-cli migrate up
cargo run
# or for production
cargo run --release
curl -X POST http://localhost:3000/users -H "Content-Type: application/json" -d '{"username":"aaa"}'
curl -X POST http://localhost:3000/users -H "Content-Type: application/json" -d '{"username":"abc"}'
curl http://localhost:3000/users\?username\=a
open http://localhost:3000/docs
open http://localhost:3000/scalar
# cargo install cargo-shuttle
cargo shuttle run
Make sure docker engine is running, otherwise:
brew install colima docker
colima start
sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock
cargo shuttle login
cargo shuttle deploy
# edit .env to use Postgres
cargo run --release
wrk --latency -t20 -c50 -d10s http://localhost:3000/users\?username\=