An open API service indexing awesome lists of open source software.

https://github.com/rhwddy/rust-api-template

Basic Rust JSON API template, cookie-based authentication included, made with axum and SQLx.
https://github.com/rhwddy/rust-api-template

axum cookiesession rust session-cookie sqlx tokio-rs uuid

Last synced: 11 months ago
JSON representation

Basic Rust JSON API template, cookie-based authentication included, made with axum and SQLx.

Awesome Lists containing this project

README

          

# Rust JSON API template
## Main Crates:
- ### [SQLx](https://docs.rs/sqlx)
SQLx is an async, pure Rust SQL crate featuring compile-time checked queries without a DSL.
- ### [axum](https://docs.rs/axum)
axum is a web application framework that focuses on ergonomics and modularity.

## Additional crates
[tokio](https://docs.rs/tokio) - [serde](https://docs.rs/serde) - [dotenv](https://docs.rs/dotenv) - [bcrypt](https://docs.rs/bcrypt) - [uuid](https://docs.rs/uuid)

## Environment
- ### Setup ENV variables by creating a `.env` at the root of the project:
```
POSTGRES_HOST=
POSTGRES_PORT=
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=

DATABASE_URL=

PGADMIN_DEFAULT_EMAIL=
PGADMIN_DEFAULT_PASSWORD=

SIGNED_JAR_KEY=
```
- ### Install SQLx CLI
Run `cargo install sqlx-cli` to install it.

## Database
- ### Creating database
Run `sqlx database create` to create a new database using the params given on `.env`.

- ### Adding migrations
Use `sqlx migrate add ` to add a new `.down.sql` and `.down.sql` migration files.

- ### Running migrations
To run a your migrations use the command `sqlx migrate run`.

## Models
- Models are located in the `src/models` folder.
- Models have multiple structs that aid on defining the JSON body of a certain request or the return of a query.
- Model structs that are supposed to be used in a JSON body derive: `#[derive(Debug, Deserialize, Serialize)]`
- Model structs that are used on queries derive: `#[derive(Debug, Deserialize, Serialize, Clone)]`

## Handlers
- Handler are located in the `src/handlers` folder.
- Handlers are supposed to just call queries.

## Routes
- Routes are located in the `src/routes` folder.
- Routes should appoint directly to an handler method, for example: `route("/users", post(user::create))`

## Config
- Config are located in the `src/config` folder.
- Contains the configuration for the App's Database, App state and Router

## Utils
- Utils are located in the `src/utils` folder.

## Structs
- Structs are located in the `src/structs` folder.
- Unlike model structs, the structs located here are meant for the internal functions of the app.

## Features
- This template comes with an user [model](https://github.com/rhwd/rust-api-template?tab=readme-ov-file#models), [handler](https://github.com/rhwd/rust-api-template?tab=readme-ov-file#handlers) and [routes](https://github.com/rhwd/rust-api-template?tab=readme-ov-file#routes) setup.
- User has a simple signed cookie based authentication setup.