fastapi-clean-architecture
fastapi-clean-architecture copied to clipboard
Base FastAPI Project to create general api.
fastapi-clean-architecture
description
Base FastAPI project for applying general RestAPI Application cases.
concept
- Minimal functionality.
- Convincing architecture.
- Easy to read.
- Compatibility.
- Versatility.
base models
- user
- post [user (1 : n) post]
- tag [post (n : n) tag]
integrated with
- Python3.9+
- Fastapi 0.78.0
- Database
- MySQL5.7+
- Migration with alembic
- pytest with real DB
- Load with two ways (eager, lazy)
- Modeling with schema (1:1, 1:n, n:n)
- dependency-injector
- service-repository pattern
- JWT authentication
- role separation each endpoint
- Deployment
- container environment(k8s, docker)
- raw WAS(Web Application Server)
commands
- db(alembic)
-
alembic upgrade head
: apply every migrations -
alembic downgrade base
: rollback every migrations -
alembic revision --autogenerate -m "revision_name"
: create new migration -
alembic history
: get alembic revision history
-
- How to migration
- Create or modify models from
app/model/*.py
-
alembic -x ENV=[dev|stage|prod] revision --autogenerate -m "revision_name"
- Check auto generated migration file from
app/migrations/versions/*.py
-
alembic -x ENV=[dev|stage|prod] upgrade head
If ENV does not exist, it will be applied to the test.
- Create or modify models from
- server
-
uvicorn app.main:app --reload
: base - options
- host:
--host 0.0.0.0
- port:
--port 8000
- host:
-
- test
-
pytest
: base -
pytest --cov=app --cov-report=term-missing
: coverage with stdout -
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