This repository contains a sample Python REST API implemented according to hexagonal architecture.
project_dir/
├── src/
│ ├── sms/
│ │ ├── adapters/
│ │ │ ├── db/
│ │ │ │ ├── migrations/
│ │ │ │ │ └── versions/
│ │ │ │ │ └── a15200892d2f_add_product_permissions.py
│ │ │ │ ├── __init__.py
│ │ │ │ └── orm.py
│ │ │ ├── entry_points/
│ │ │ │ ├── api/
│ │ │ │ │ ├── v1/
│ │ │ │ │ │ └── brand.py
│ │ │ │ │ ├── app.py
│ │ │ │ │ ├── base.py
│ │ │ │ │ └── main.py
│ │ │ │ └── __init__.py
│ │ │ ├── repositories/
│ │ │ │ │── brand.py
│ │ │ │ └── __init__.py
│ │ │ ├── unit_of_works.py
│ │ │ └── __init__.py
│ │ ├── config/
│ │ │ ├── containers.py
│ │ │ └── settings.py
│ │ ├── core/
│ │ │ ├── domain/
│ │ │ │ ├── dtos.py
│ │ │ │ ├── models.py
│ │ │ │ └── __init__.py
│ │ │ ├── ports/
│ │ │ │ ├── repositories.py
│ │ │ │ ├── services.py
│ │ │ │ ├── unit_of_works.py
│ │ │ │ └── __init__.py
│ │ │ ├── services/
│ │ │ │ ├── brand_service_impl.py
│ │ │ │ └── __init__.py
│ │ │ ├── helpers.py
│ │ │ ├── constants.py
│ │ │ └── __init__.py
│ │ └── __init__.py
├── tests/
│ └── __init__.py
├── .gitignore
├── requirements.txt
├── alembic.ini
└── README.md
src/
Contains the source code of the project
sms/
Main application directoryadapters/
Contains the implementations of the adapters (e.g. database, API, etc.)db/
Database related code (e.g. ORM, migrations, etc.)entry_points/
Entry points of the application, in this project we use just FastAPI
config/
Configuration files likesettings.py
for database connection andcontainers.py
for dependency injectioncore/
Core domain logicdomain/
Domain models and DTOsports/
Interfaces or abstract classes for the core domain.services/
Service implementationshelpers.py
Helper functions and utilitiesconstants.py
Constant values used across the application
- SQLAlchemy: The way SQLAlchemy is implemented in this project is called the Data Mapper Pattern. This pattern is characterized by the use of a separate layer of mappers that move data between objects and a database while keeping them independent of each other.
- dependency-injector for Dependency Injection: DI helps in decoupling the core business logic from the infrastructure code. This makes the core logic independent of external systems, which can be easily swapped or modified without affecting the core.