ndelvalle/rustapi
Sobre
🚀 RESTful Rust API Template / Boilerplate
Dados e análiseO que você pode fazer
- 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
Crescimento medido de estrelas
- em 24 h
- 0
- em 7 dias
- +2
- em 15 dias
- +2
- em 1 mês
- Coletando histórico
Variações líquidas em períodos sobrepostos, não totais diários. Medições ausentes ficam em branco. Estrelas indicam interesse, não qualidade.
README.md
Originalrustapi
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
Explore projetos relacionados
Carregando…