Kickstart your backend application with a pre-configured FastAPI + SQLAlchemy template
- FastAPI as web framework
- SQLAlchemy as the ORM
- PostreSQL as database engine
- Alembic for database migrations
- JWT for auth
- Poetry as package manager
- PyTest for unit testing
- Black for code formatting
- Ruff for linting
- Docker for containerization
- Github Actions for CI checks (lint, test, coverage)
Clone repo, remove git history:
git clone https://github.com/minyma-technologies/python-fastapi-starter
cd python-fastapi-starter
rm -rf ./.git
git init
Alternatively, on GitHub hit Use this template
to create a new repository based on this, but without the git history. You can then clone the new repository to your machine.
- this template uses Poetry. You can install it like so:
pip install poetry
- once installed run
poetry install
to install the dependencies
poe dev
or, for live preloads:
poe watch
docker-compose up --build
Poetry is an alternative python package manager. From experience, the default python package manager pip
and the virtual environment tool venv
are poorly designed and often cause package configuration issues. Enter poetry: a modern package manager with a more sane API.
- create virtual environment:
poetry shell
- add dependency:
poetry add <pacakage>
- add dev dependency:
poetry add <pacakage> -G dev
- the template is set up with alembic, an automatic migrations tool for SQLAlchemy.
- To automatically run migrations against the database run
poe db_pull
- To automatically genrate migrations against the database run
poe db_push <message>
There are three different environments preconfigured: test
, dev
, and prod
. To switch environments set APP_ENV
to one of the above environment names. Use dev.env
(default, for dev
), test.env
and .prod.env
for storing the values. Note the .
before prod.env
: adding a dot before any file will automatically git ignore it.
As a drop-in replacement for npm run ...
you can use poe
. This way you can change development scripts in one place and each collaborator can then use the updated script, without thinking about flags and options.
Avaialble scripts are:
Command | Description |
---|---|
poe fix |
Run black, ruff and mypy to fix lint issues |
poe check |
Run black, ruff and mypyp to check for lint issues |
poe test |
Run unit tests |
poe coverage |
Run coverage test |
poe prod |
Start the app in prod mode |
poe dev |
Start the app in dev with mode |
poe watch |
Start the app in dev mode with live reload |
poe db_push <message> |
Autogenerate database migration |
poe db_pull |
Apply migrations |
- service layer tests