Skip to content
Public template

About

πŸš€ RESTful Rust API Template / Boilerplate

Topics

Resources

Stars

551 stars

Watchers

4 watching

Forks

Latest commit

Β 

History

462 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

rustapi

Tests

RESTful API template built with Rust lang. It uses a PostgreSQL database through sqlx and the Axum HTTP framework.

Requirements

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

MIT

About

πŸš€ RESTful Rust API Template / Boilerplate

Topics

Resources

Stars

551 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages