This is a project that is meant to explore the capabilities of FastAPI by building a real-world application - in this case a simple twitter clone.
The frontend NextJS Application code can be found here https://github.com/dericf/twitter-clone-frontend
You can see the live, hosted, auto-generated api documentation here
Make sure you have docker installed on your local machine.
Generate an application secret key by running openssl rand -hex 32
create a .env
file and add the following environment variables
export LOCAL_POSTGRES_URL="postgresql://admin:secret@localhost:5433/twitterdb"
export LOCAL_DOCKER_INTERNAL_POSTGRES_URL="postgresql://admin:secret@db:5432/twitterdb"
export PRODUCTION_POSTGRES_URL="postgresql://admin:secret@<cloud-host>:5432/twitterdb"
export ENV=development
export SECRET_KEY="ASuperSecretKey"
export FIRST_SUPERUSER_EMAIL="super@user.com"
export FIRST_SUPERUSER_PASSWORD="asupersecretpassword"
export EMAIL_TEST_USER=john@doe.com
export SEND_GRID_API_KEY=<Your Send Grid API Key>
export PRODUCTION_CLIENT_HOST_URL="localhost:3000"
export SEND_GRID_FROM_EMAIL="account-verification@your-domain.com"
export PRODUCTION_DOMAIN_HOST="your-api-host-domain.com"
run source .env
to apply the environment variables
All you need to run is docker-compose -f docker-compose-dev.yml up --build
and it will build the docker image based on Dockerfile-dev
and will automatically handle hot-reloading while developing.
Run alembic downgrade base
Run alembic upgrade head
Run alembic revision -m "version tag"
navigate to localhost:8001/docs
or localhost:8001/redocs
Create a new Linode Server
Add CNAME record to point to that server
SSH into your server ssh root@your-domain.com
run apt update
to update package listing
run apt upgrade
to actually install all latest packages
TODO: apt install haveged
rsync -a --exclude 'pgdata' --exclude '__pycache__' --exclude 'venv' ./* root@twitter-clone-fastapi.programmertutor.com:/root/code/twitter-clone-server-fastapi
Note this doesn't send the .env file - must create that manually on the server once.
create the docker-compose.traefik.yml file
create the docker-compose.override.yml file
create 3 new .env variables (USERNAME, PASSWORD and HASHED_PASSWORD)
get the value of HASHED_PASSWORD
with echo $(openssl passwd -apr1 $PASSWORD)
Create the traefik public network with docker network create traefik-public
Run traefik docker container with docker-compose -f docker-compose.traefik.yml up -d
Run the fastapi server with docker-compose -f docker-compose-prod.yml up --build -d
Create a new Postgres Database on AWS RDS and update the PRODUCTION_POSTGRES_URL
in your .env
file
https://fastapi.tiangolo.com/python-types/
https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html
https://fastapi.tiangolo.com/tutorial/sql-databases/
https://fastapi.tiangolo.com/deployment/manually/
https://fastapi.tiangolo.com/deployment/docker/
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#format
https://code.visualstudio.com/docs/remote/containers
FastAPI — How to add basic and cookie authentication (by Nils de Bruin)