fastapi_quickstart
fastapi_quickstart copied to clipboard
Initial FastAPI project with SQLAlchemy (asyncpg), Alembic, Pydantic, Pytest, Poetry Gunicorn, Docker, docker-compose, Ruff, coverage, logging
fastapi_quickstart (In development, WIP)!!!
Initial FastAPI project with SQLAlchemy (asyncpg), Alembic, Pydantic v2, Pytest, Poetry, Gunicorn, Docker, docker-compose, ruff, black, isort, flake8, coverage, factory-boy, pytest-alembic, pydantic-factories.
Dependencies:
- Python 3.12 (Main programming language for Back-end)
- Poetry (For package and dependencies management)
- Docker & docker-compose (For containerization of app)
- Taskfile (Commands runner)
Setup
Create .env
file (based on example.env)
cp example.env .env
Setup environment
poetry env use python3.11
Activate environment
poetry shell
Install packages
poetry install
Run all containers (PostgreSQL, PGAdmin, Redis, RedisInsights)
docker-compose up -d
Run Back-end
docker-compose (local container)
It also possible to uncomment backend service inside docker-compose.yml and run through the docker compose.
Note: Set
POSTGRES_HOST
topostgres
(POSTGRES_HOST=postgres
) inside .env file
Uvicorn (local)
poetry run python -m apps
With Gunicorn & UvicornWorker (like a prod)
task run
Tech stack
Package & Dependencies Management
- poetry (with pyproject.toml)
Infrastructure
- Docker
- docker-compose
- db (PostgreSQL latest)
- pgadmin (PGAdmin — GUI for PostgreSQL simplifies query creation, profiling and management, debugging)
- redis (Redis latest)
- redis_insights (Redis Insights — GUI for Redis)
- backend (Commented — possible to run Back-end via docker-compose)
Tests
- pytest (with pytest-asyncio)
- pytest-cov (for run tests with coverage)
- pytest-mock (to use
mocker
fixture) - pytest-randomly (to random sort tests in runtime)
- pytest-clarity (for better tests fails descriptions)
- Faker (to generate random data)
- pytest-alembic (to run tests on migrations)
- factory-boy (to generate model factories in db)
Debugging
- iPython (enhanced console for Python)
Linters & Formatters
- ruff (Rust based linter & formatter)
- xenon (code linter, complexity linter)
- mypy (code linter, type annotations linter)
Frameworks
- FastAPI (Starlette) (ASGI web framework)
- typer (CLI creation framework)
DB drivers & tools
- SQLAlchemy (ORM, Core, db schema declaration)
- alembic (DB migrations tool)
- psycopg2-binary (sync driver for working with PostgreSQL)
- asyncpg (async driver for working with PostgreSQL)
- aioredis (async driver for working with Redis)
Extra libraries
- httpx (async client library)
- orjson (fast JSON serialization/deserialization)
- pydantic v2 (JSON & data validation tool)
- uvicorn (ASGI web server implementation)
- gunicorn (process management tool)
- bcrypt (hashing library, passwords hashing)
- PyJWT (library for working with JWT tokens)