/FastAPI

Primary LanguagePython

FastAPI

Training References:

Packages Used

Local Setup

# Create virtual environment
python3 -m venv venv
# Activate it
source venv/bin/activate
# make sure pip is up to date
pip install --upgrade pip
# install project's requirements
pip install -r requirements.txt
# start database engine
docker compose up -d
# Initialize alembic file structure
alembic init alembic
# copy the project ini file
cp app/alembic.env.py alembic/env.py
# Autogenerate the database schema creation file
alembic revision --autogenerate -m "Application schema"
# Execute the schema creation
alembic upgrade head
# Start Web server: 
uvicorn app.main:app --reload

Heroku setup

# heroku login via cli 
# Email: your email
# Password: heroku generated token
heroku login -i
# Create app with global name. Only do this once
heroku apps:create fastapi-dessyd
# push content to herou
git push heroku main
# Access the app
# go to https://fastapi-dessyd.herokuapp.com/

# Add postgres
heroku addons:create heroku-postgresql:hobby-dev

Environment variables

All environment variables are stored in a .env file having the following structure:

# DB section
DATABASE_DRIVER=postgresql
DATABASE_HOSTNAME=<target host>
DATABASE_PORT=5432
DATABASE_NAME=<DB name>

DATABASE_USERNAME=<db user>
DATABASE_PASSWORD=<db password

# oauth section
# to get a SECRET_KEY string run:
# openssl rand -hex 32
SECRET_KEY=<oauth session secret key>
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60

Ubuntu

Set environment from .env syntax ( no export)

set -o allexport; source ~/.env ; set +o allexport