Skip to content

Task Tracker backend using FastAPI with bearer token authentication

Notifications You must be signed in to change notification settings

nickshanks347/task-tracker-backend-fastapi

Repository files navigation

task-tracker-backend-fastapi

Build & Push CodeQL codecov

This is a simple task tracker backend using FastAPI. It uses a simple bearer token authentication scheme with SQLite as the database.

Installation

First, clone the repository:

git clone https://github.com/nickshanks347/task-tracker-backend-fastapi

cd into task-tracker-backend-fastapi:

cd task-tracker-backend-fastapi

Install dependencies:

poetry install --no-dev

Optionally install development dependencies too:

poetry install

Copy the config file:

cd data
cp config_example.env config.env

Configuration

The application needs config.env in order to start correctly.

Included in the repository is a config_example.env, stored in the data directory. It has this format:

JWT_SECRET_KEY=secret
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
ENABLE_REGISTRATIONS=1
RELOAD=1
HOST=0.0.0.0
PORT=8000
  1. JWT_SECRET_KEY contains the secret key for the JWT authentication scheme.
  2. ALGORITHM contains the algorithm for the JWT encryption scheme.
  3. ACCESS_TOKEN_EXPIRE_MINUTES contains the number of minutes the access token is valid for.
  4. ENABLE_REGISTRATIONS contains whether or not registrations are enabled.
  5. RELOAD contains whether or not the application should reload the Python files on saving (usually only enabled for debugging/development).
  6. HOST contains the hostname to bind the application to.
  7. PORT contains the port to bind the application to.

When the application starts, it will use python-dotenv to set the environment variables listed above. The application then reads the required environment variables and loads them into the application.

To generate a secure value for JWT_SECRET_KEY, use the following command:

openssl rand -hex 32

Running the application

To run the application, simply enter task-tracker-backend-fastapi and run the following command:

python3 main.py

Docker

The application is available on DockerHub at https://hub.docker.com/r/nickshanks347/todo-fastapi. You can pull the latest application with docker pull nickshanks347/todo-fastapi:latest. Each tag corresponds to a short Git commit hash.

Using the environment variables above, you can run the Docker image with the following command:

docker run -p 8000:8000 --name todo-fastapi -e JWT_SECRET_KEY=secret -e ALGORITHM=HS256 -e ACCESS_TOKEN_EXPIRE_MINUTES=30 -e ENABLE_REGISTRATIONS=1 -e RELOAD=0 -e HOST=0.0.0.0 -e PORT=8000 -v $PWD/data:/code/data nickshanks347/todo-fastapi:latest

Again, be sure to use a secure value for JWT_SECRET_KEY.

Building Docker image

If you want to build the image locally, use the included Dockerfile:

docker build -t todo-fastapi .

About

Task Tracker backend using FastAPI with bearer token authentication

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published