This repository contains a sample Python REST API implemented according to hexagonal architecture.
make install # Install dependencies (requires .venv)
make format # Format code (isort, autoflake, black)
make test # Run all tests with pytest
make migrations # Create alembic migration
make migrate # Run alembic migrations
make run # Start FastAPI server on port 8000src/sms/
βββ core/ # Domain logic (business rules, independent of infrastructure)
β βββ domain/ # Models (dataclasses) and DTOs (Pydantic)
β βββ ports/ # Interfaces: repositories.py, services.py, unit_of_works.py
β βββ services/ # Service implementations (business logic)
βββ adapters/ # Infrastructure implementations
β βββ db/orm.py # SQLAlchemy Data Mapper (imperative mapping)
β βββ repositories/ # Repository implementations
β βββ unit_of_works.py # UoW implementations with AsyncSession
β βββ entry_points/api/ # FastAPI endpoints
βββ config/
βββ containers.py # dependency-injector DI container
βββ settings.py # Database and app configuration
- SQLAlchemy Data Mapper Pattern: Tables are defined separately from domain models (dataclasses) and mapped imperatively in
orm.py. Domain models remain free of ORM dependencies. - Dependency Injection: Uses
dependency-injectorlibrary to decouple core business logic from infrastructure code. This makes the core logic independent of external systems, which can be easily swapped or modified without affecting the core. - Unit of Work: Transaction management via async context managers.
- Soft Deletes: All entities have
deleted_atfield for logical deletion. - Fakes for Testing: In-memory fake implementations of repositories in
adapters/repositories/fakes/for unit testing services without a database.
- PostgreSQL with asyncpg driver
- Production: port 5435, Test: port 5436
- Alembic for migrations