Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/marirs/rocket-jwt

Rocket api server with JWT
https://github.com/marirs/rocket-jwt

jwt jwt-auth jwt-authentication rocket rust rust-lang

Last synced: about 1 month ago
JSON representation

Rocket api server with JWT

Awesome Lists containing this project

README

        

# rocket-jwt
[![CI](https://github.com/marirs/rocket-jwt/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/marirs/rocket-jwt/actions/workflows/ci.yml)
[![Build Status](https://travis-ci.com/marirs/rocket-jwt.svg?branch=master)](https://travis-ci.com/marirs/rocket-jwt)
![GitHub](https://img.shields.io/github/license/marirs/rocket-jwt)

A barebones [Rocket](https://rocket.rs) API with [JWT](https://jwt.io) authentication and
database integration.

The database integration can be switched between [sqlite](https://www.sqlite.org/index.html) and [postgres](https://www.postgresql.org/) with a simple feature flag.

## Requirement
- Rust version 1.52 or newer.
- Diesel CLI with `postgres` or `sqlite` features.
- A running [PostgreSQL](https://www.postgresql.org/) or [Sqlite](https://www.sqlite.org/index.html) backend.

## Setup environment

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

``` bash
source .env
```

## Build locally

Run the following command to fulfill the requirements :

``` bash
# 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)`:
```bash
cargo build --release
```

or build with `postgresql` database:

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

## Create the first user
```bash
# 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
``` http
GET /docs
```

- Create a new user
```http
POST /users
Authorization: Bearer
{
"username": "string",
"password": "string",
"email": "string",
"is_admin": boolean
}
```

- Authenticate
```http
POST /auth
{
"username": "string",
"password": "string"
}
```

- Get a list of users
```http
GET /users
Authorization: Bearer
```

- Delete an existing user
```http
DELETE /users/
Authorization: Bearer
```

---
License: MIT