ndelvalle/rustapi
ุญูู
๐ RESTful Rust API Template / Boilerplate
ุงูุจูุงูุงุช ูุงูุชุญูููุงุชู ุง ูู ููู ูุนูู
- 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
ุงููู ู ุงูู ูุงุณ ูููุฌูู
- ุฎูุงู 24 ุณุงุนุฉ
- ู
- ุฎูุงู 7 ุฃูุงู
- +ูข
- ุฎูุงู 15 ููู ูุง
- +ูข
- ุฎูุงู ุดูุฑ
- ุฌุงุฑู ุฌู ุน ุงูุณุฌู
ุชุบููุฑ ุตุงูู ุงููุฌูู ุฎูุงู ูุชุฑุงุช ู ุชุฏุงุฎูุฉุ ูููุณ ู ุฌู ูุนูุง ููู ููุง. ุชุจูู ุงูููุงุณุงุช ุงูู ูููุฏุฉ ูุงุฑุบุฉ. ุงููุฌูู ุชููุณ ุงูุงูุชู ุงู ูุง ุฌูุฏุฉ ุงูุจุฑู ุฌูุงุช.
README.md
ุงูุฃุตูrustapi
RESTful API template built with Rust lang. It uses a PostgreSQL database through sqlx and the Axum HTTP framework.
Requirements
- Rust
- PostgreSQL 18 or newer, for its built-in
uuidv7()
How to use this template
To use this template as your project starting point, click "Use this template" at the top of this page, or click here.
Feature highlights
- 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
Project structure
โโโ 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 Tests
Run
Create 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.
API documentation
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.
Test
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).
Contributing
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.
License
ุงุณุชูุดู ู ุดุงุฑูุน ู ุดุงุจูุฉ ูู ุฑุชุจุทุฉ
ุฌุงุฑู ุงูุชุญู ููโฆ