Implementing the hexagonal architecture, a simplified version of the Clean architecture from uncle bob.
- Using fastapi, a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
- An async database driver, using encode and asyncpg, the fastest Postgres database driver for python
- Environment and dependencies handled by poetry
- Automatic code formatting using Black and automatic sorting of imports using isort
You can run the app in ways: in a docker container or locally. I recommend to try the docker method first as it's easier to set up. Revert to the local method if docker is eating too much of your RAM.
The docker container way uses VS Code Remote containers. Like any docker based development, the main benefit is the automated setup - you don't have to worry about
VS Code remote containers adds a few benefits to this:
- Code auto-completion uses the python version and libraries installed in the container (normally it uses what is on your local machine)
- The configuration specified which IDE extension should be installed, so that we have a consistent dev experience
- Easily use the vscode debugger
- Docker installed (and running)
- Using VS Code as your IDE
- Start VS Code in the folder.
- Install the "Remote - Containers" extension
- Add your ssh credentials to the ssh agent. This ensures you'll be able to push/pull from the container
ex :
ssh-add $HOME/.ssh/id_rsa
(you might have to modify this if you're not using the default key) Note: does NOT work if you're using a passphrase - Click on the green box at the lower left corner of VS Code and select "Remote-Containers: Open in Container"
- Wait for the container to build
- Update the database schema and genrate the SQLalchmey tables (
poetry run task manage sync-db
) - Run the app with the development server (
poetry run task app
)
Go to http://127.0.0.1:8000/doc for the swagger doc
- Run the app with the development server (
poetry run task app
)
:
- Install Python 3.8: I recommend using PyEnv
- Install poetry
- Install postgresapp
- Start your IDE in the folder.
- Create a copy of the settings (
cp .dist.env .env
). Modify the database user (DB_USER
) - unless you're bob - , it should be your system user if you're using postgresapp. - Install depencies and create a virtual environment (
poetry install
) - Activate the virtual environment (
poetry shell
) - Activate pre-commit hooks (after
poetry shell
, runpre-commit install
). - Update the database schema and genrate the SQLalchmey tables (
poetry run task manage sync-db
) - Run the app with the development server (
poetry run task app
)
Go to http://127.0.0.1:8000/doc for the swagger doc
- Activate the virtual environment (
poetry shell
) (might be automatic depending on your IDE) - Run the app with the development server (
poetry run task app
)
- Update the DDL (
database-schema.sql
) - Run
poetry run task manage sync-db
to update the database and the SQLA tables - Create a query file & a class to query the new table (
infrastructure/database/queries
) - Create the schemas in the
domain
folder - Create the service in the
domain
folder - Create a service factory in the
servicesfac.py
file - Create a new router as a new file in the
api/endpoints
folder) - Link the router to the main router in
api.py
- poetry run task tests
api
: the http layer. Deserialize request. Chooses which status code to return. Serializes the response. No logic behind that.core
: config (app and gunicorn) and db initializationdomain
: Business logic. Each entity has one subfolder, with 2 files: a schema file, and a service file. Schema file defines how to ser/des the business objects, while the service contains all the business logicinfrastructure
: Code to interface with any infra (database, cache, queue, other apis)infrastructure/database
: Database code. Contains:migrations
: DDL to setup database, as.sql
,models
: the SQLAlchemy core table representation of the database,queries
: Classes to manage all the data access.tasks
: Contains any asynchronous task docutils
: Generic code, reusable for any service. to be moved in a library at a later pointroot
:main
application factory,manage
python scripts
All commands to be executed after poetry shell
if your IDE does'nt do it automatically
- poetry run app : Runs the app with the dev server
- poetry run app-prod: Runs the app with the production server
- poetry run manage db-sync: Syncs changes to the DDL file (
database-schema.sql
) to the database and the sqlalchemy models - poetry run test : Runs the test suite
- poetry version <patch|minor|major>: updates the version
- poetry run task manage commit-new-version: updates git tag, add changed files, commits, pushes (including version)
Couple of caveats:
- some endpoints still are using the old sync model