RESTful API template built with Rust lang. It uses a PostgreSQL database through sqlx and the Axum HTTP framework.
- Rust
- PostgreSQL 18 or newer, for its built-in
uuidv7()
To use this template as your project starting point, click "Use this template" at the top of this page, or click here.
- Authentication. Based on jsonwebtoken
- Layered configuration. Based on config-rs
- SQL migrations. The database generates UUIDv7 IDs and maintains the timestamps
- Logs. Based on tracing, with the request ID on every line logged while handling a request
- Error handling. Every error has the same JSON body, and validation errors list the invalid fields
- Graceful shutdown, request IDs, and a configurable request timeout and body size limit
- Status route that checks the database
- Pagination
- E2E Tests
- CI based on Github actions
- Dependabot configuration
βββ Cargo.lock
βββ Cargo.toml
βββ LICENSE
βββ Makefile
βββ README.md
βββ config
β βββ default.json # Default configuration
β βββ development.json # Development configuration (Overwrites the default)
β βββ production.json # Production configuration (Overwrites the default)
β βββ test.json # Test configuration (Overwrites the default)
βββ migrations # SQL migrations, applied on startup
βββ src
β βββ app.rs # Router, middleware and the context shared with the handlers
β βββ common # Helpers not related to the business model
β β βββ authenticate_request.rs
β β βββ custom_response.rs
β β βββ date.rs
β β βββ mod.rs
β β βββ pagination.rs
β β βββ to_uuid.rs
β β βββ token.rs
β β βββ validated_json.rs
β βββ database.rs # Connection pool and migrations
β βββ errors.rs
β βββ lib.rs # The API as a library, shared by the binary and the tests
β βββ logger.rs
β βββ main.rs # Loads the settings and starts the server
β βββ models # Structs and the SQL queries that load and store them
β β βββ cat.rs
β β βββ mod.rs
β β βββ user.rs
β βββ routes # Handlers with their request and response bodies
β β βββ cat.rs
β β βββ mod.rs
β β βββ status.rs
β β βββ user.rs
β βββ settings.rs
βββ tests
βββ api # E2E TestsCreate the database and start the server. Migrations are applied on startup.
make db-create
make run
The Makefile has the other commands, such as make check to run the same
checks as CI and make db-reset to start over with an empty database.
Configuration is layered. config/default.json is loaded first, then
config/<RUN_MODE>.json (development when RUN_MODE is not set), then an
optional config/local.json, and finally environment variables, where __
separates the levels. For example, DATABASE__URI overrides database.uri.
Only development and test ship with a JWT secret. In any other mode the server
refuses to start until AUTH__SECRET is set.
Browsers may only call the API from the origins in cors.allowed_origins. It
is * (any origin) in development and empty everywhere else, so set it for
production, for example CORS__ALLOWED_ORIGINS=https://app.example.com,https://admin.example.com.
This template is deliberately minimal, so it does not generate or serve an OpenAPI specification. Most private APIs never need to publish one. If yours does, utoipa generates it from the handlers and integrates well with axum.
To run tests make sure PostgreSQL is up and running.
make test
Every test starts its own instance of the API on a random port, backed by a
temporary database that is created and migrated for that test, so tests run in
parallel. The databases are created on the server that DATABASE_URL points
to, which defaults to postgres://localhost:5432/postgres (see
.cargo/config.toml).
Contributors are welcome, please fork and send pull requests! If you find a bug or have any ideas on how to improve this project please submit an issue.