Skip to content

Instantly share code, notes, and snippets.

@igorbenav
Last active February 6, 2024 10:07
Show Gist options
  • Save igorbenav/48ad745120c3f77817e094f3a609111a to your computer and use it in GitHub Desktop.
Save igorbenav/48ad745120c3f77817e094f3a609111a to your computer and use it in GitHub Desktop.
Running FastAPI-boilerplate Locally with Uvicorn
# ------------- app settings -------------
APP_NAME="My Project"
APP_DESCRIPTION="My Project Description"
APP_VERSION="0.1"
CONTACT_NAME="Me"
CONTACT_EMAIL="my.email@example.com"
LICENSE_NAME="MIT"
# ------------- database -------------
POSTGRES_USER="postgres"
POSTGRES_PASSWORD=1234
POSTGRES_SERVER="db"
POSTGRES_PORT=5432
POSTGRES_DB="postgres"
POSTGRES_ASYNC_PREFIX="postgresql+asyncpg://"
# ------------- crypt -------------
SECRET_KEY=de2132a4a3a029d6a93a2aefcb519f0219990f92ca258a7c5ed938a444dbe1c8
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60
# ------------- admin -------------
ADMIN_NAME="admin"
ADMIN_EMAIL="admin@example.com"
ADMIN_USERNAME="admin"
ADMIN_PASSWORD="Str1ngst!"
# ------------- redis cache -------------
REDIS_CACHE_HOST="redis"
REDIS_CACHE_PORT=6379
# ------------- redis queue -------------
REDIS_QUEUE_HOST="redis"
REDIS_QUEUE_PORT=6379
# ------------- redis rate limit -------------
REDIS_RATE_LIMIT_HOST="redis"
REDIS_RATE_LIMIT_PORT=6379
# ------------- client side cache -------------
CLIENT_CACHE_MAX_AGE=60
# ------------- test -------------
TEST_NAME="Tester User"
TEST_EMAIL="test@tester.com"
TEST_USERNAME="testeruser"
TEST_PASSWORD="Str1ng$t"
# ------------- environment -------------
ENVIRONMENT="local"
# ------------- first tier -------------
TIER_NAME="free"
version: '3.8'
services:
web:
build:
context: .
dockerfile: Dockerfile
# -------- Both of the following commands should be commented to run with nginx --------
# -------- replace with comment to run with gunicorn --------
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
# command: gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
env_file:
- ./src/.env
# -------- replace with expose if you are using nginx --------
ports:
- "8000:8000"
# expose:
# - "8000"
depends_on:
- db
- redis
volumes:
- ./src/app:/code/app
- ./src/.env:/code/.env
worker:
build:
context: .
dockerfile: Dockerfile
command: arq app.core.worker.settings.WorkerSettings
env_file:
- ./src/.env
depends_on:
- db
- redis
volumes:
- ./src/app:/code/app
- ./src/.env:/code/.env
db:
image: postgres:13
env_file:
- ./src/.env
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- "5432"
redis:
image: redis:alpine
volumes:
- redis-data:/data
expose:
- "6379"
#-------- uncomment to run with nginx --------
# nginx:
# image: nginx:latest
# ports:
# - "80:80"
# volumes:
# - ./default.conf:/etc/nginx/conf.d/default.conf
# depends_on:
# - web
#-------- uncomment to create first superuser --------
create_superuser:
build:
context: .
dockerfile: Dockerfile
env_file:
- ./src/.env
depends_on:
- db
- web
command: python -m src.scripts.create_first_superuser
volumes:
- ./src:/code/src
#-------- uncomment to run tests --------
pytest:
build:
context: .
dockerfile: Dockerfile
env_file:
- ./src/.env
depends_on:
- db
- create_superuser
- redis
command: python -m pytest ./tests
volumes:
- .:/code
#-------- uncomment to create first tier --------
# create_tier:
# build:
# context: .
# dockerfile: Dockerfile
# env_file:
# - ./src/.env
# depends_on:
# - create_superuser
# - db
# - web
# command: python -m src.scripts.create_first_tier
# volumes:
# - ./src:/code/src
volumes:
postgres-data:
redis-data:
# --------- requirements ---------
FROM python:3.11 as requirements-stage
WORKDIR /tmp
RUN pip install poetry
COPY ./pyproject.toml ./poetry.lock* /tmp/
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
# --------- final image build ---------
FROM python:3.11
WORKDIR /code
COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./src/app /code/app
# -------- replace with comment to run with gunicorn --------
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker". "-b", "0.0.0.0:8000"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment