/rocket-jwt

Rocket api server with JWT

Primary LanguageRustMIT LicenseMIT

rocket-jwt

CI Build Status GitHub

A barebones Rocket API with JWT authentication and database integration.

The database integration can be switched between sqlite and postgres with a simple feature flag.

Requirement

  • Rust version 1.52 or newer.
  • Diesel CLI with postgres or sqlite features.
  • A running PostgreSQL or Sqlite backend.

Setup environment

A .env.{db} at the root directory exposes environment both used by dieseland the project itself.
Rename it to .env then set all the environment variables before running the following commands :

source .env

Build locally

Run the following command to fulfill the requirements :

# Install Rust and cargo alongside rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Install ORM and query builder
cargo install diesel_cli --no-default-features --features "postgres sqlite"

# Run migrations
diesel setup 
diesel migration run

Then build the project with default database (sqlite):

cargo build --release

or build with postgresql database:

$ cargo build --release --no-default-features --features postgres

Create the first user

# Build the create-user binary
cargo build --release --features create-user-binary

# Execute the binary to create the first admin user
../target/release/create-user

Available feature flags

You can use the feature flag to switch between database use:

  • sqlite (default)
  • postgres

To build the create-user binary

  • create-user-binary

Switching the database at the backend

Any time you want to perform a database switch, you have to:

  1. Install and run the desired database on your machine if is not already the case
  2. Run migrations
  3. Build the project with the respective feature flag.

Usage

To print the project usage, an option -h is available.

API routes

  • Get Swagger docs
GET /docs
  • Create a new user
POST /users
Authorization: Bearer <token>
{
"username": "string",
"password": "string",
"email": "string",
"is_admin": boolean
}
  • Authenticate
POST /auth
{
"username": "string",
"password": "string"
}
  • Get a list of users
GET /users
Authorization: Bearer <token>
  • Delete an existing user
DELETE /users/<username>
Authorization: Bearer <token>

License: MIT