This is a simple task tracker backend using FastAPI. It uses a simple bearer token authentication scheme with SQLite as the database.
First, clone the repository:
git clone https://github.com/nickshanks347/task-tracker-backend-fastapicd into task-tracker-backend-fastapi:
cd task-tracker-backend-fastapiInstall dependencies:
poetry install --no-devOptionally install development dependencies too:
poetry installCopy the config file:
cd data
cp config_example.env config.envThe 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=8000JWT_SECRET_KEYcontains the secret key for the JWT authentication scheme.ALGORITHMcontains the algorithm for the JWT encryption scheme.ACCESS_TOKEN_EXPIRE_MINUTEScontains the number of minutes the access token is valid for.ENABLE_REGISTRATIONScontains whether or not registrations are enabled.RELOADcontains whether or not the application should reload the Python files on saving (usually only enabled for debugging/development).HOSTcontains the hostname to bind the application to.PORTcontains 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 32To run the application, simply enter task-tracker-backend-fastapi and run the following command:
python3 main.pyThe 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:latestAgain, be sure to use a secure value for JWT_SECRET_KEY.
If you want to build the image locally, use the included Dockerfile:
docker build -t todo-fastapi .