Skip to content

jujumilk3/fastapi-clean-architecture

Repository files navigation

fastapi-clean-architecture

description

Base FastAPI project for applying general RestAPI Application cases. openapi-docs

concept

  1. Minimal functionality.
  2. Convincing architecture.
  3. Easy to read.
  4. Compatibility.
  5. Versatility.

base models

  1. user
  2. post [user (1 : n) post]
  3. tag [post (n : n) tag]

integrated with

  1. Python3.9+
  2. Fastapi 0.78.0
  3. Database
    1. MySQL5.7+
    2. Migration with alembic
    3. pytest with real DB
    4. Load with two ways (eager, lazy)
    5. Modeling with schema (1:1, 1:n, n:n)
  4. dependency-injector
    1. service-repository pattern
  5. JWT authentication
    1. role separation each endpoint
  6. Deployment
    1. container environment(k8s, docker)
    2. raw WAS(Web Application Server)

commands

  1. db(alembic)
    1. alembic upgrade head: apply every migrations
    2. alembic downgrade base: rollback every migrations
    3. alembic revision --autogenerate -m "revision_name": create new migration
    4. alembic history: get alembic revision history
  2. How to migration
    1. Create or modify models from app/model/*.py
    2. alembic -x ENV=[dev|stage|prod] revision --autogenerate -m "revision_name"
    3. Check auto generated migration file from app/migrations/versions/*.py
    4. alembic -x ENV=[dev|stage|prod] upgrade head
      If ENV does not exist, it will be applied to the test.
  3. server
    1. uvicorn app.main:app --reload: base
    2. options
      1. host: --host 0.0.0.0
      2. port: --port 8000
  4. test
    1. pytest: base
    2. pytest --cov=app --cov-report=term-missing: coverage with stdout
    3. pytest --cov=app --cov-report=html: coverage with html

sample env

# mysql case
ENV=dev
DB=mysql
DB_USER=root
DB_PASSWORD=qwer1234
DB_HOST=localhost
DB_PORT=3306

# postgres case
ENV=dev
DB=postgresql
DB_USER=gyu
DB_PASSWORD=
DB_HOST=localhost
DB_PORT=5432

references

  1. FastAPI official docs
  2. alembic official tutorial
  3. Dependency Injector