A simple, modular FastAPI application for managing orders. Built as a modular monolith with clean separation of concerns, PostgreSQL for persistence, Alembic for migrations, and SQLite for fast unit tests.
.
├── app/
│ ├── main.py
│ ├── api/
│ │ └── router.py
│ ├── core/
│ ├── db/
│ │ └── session.py
│ └── modules/
│ ├── orders/
│ │ ├── models.py
│ │ ├── schemas.py
│ │ ├── repository.py
│ │ ├── service.py
│ │ └── routes.py
│ └── users/
│ ├── models.py
│ ├── schemas.py
│ ├── repository.py
│ ├── service.py
│ └── routes.py
│
├── alembic/
├── tests/
│ └── unit/
├── pyproject.toml
└── README.md
It’s recommended to use a virtual environment to isolate dependencies.
python -m venv .venvLinux / macOS
source .venv/bin/activateWindows (PowerShell)
.venv\Scripts\Activate.ps1pip install --upgrade pip
pip install -e '.[dev]'Start a local PostgreSQL instance:
docker run -d \
--name postgres-dev \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=app \
-p 5432:5432 \
postgres:15Set your database connection:
export DATABASE_URL=postgresql://postgres:postgres@localhost:5432/appCreate migration:
python -m alembic revision --autogenerate -m "init"Apply migrations:
python -m alembic upgrade headuvicorn app.main:app --reloadAPI will be available at:
http://localhost:8000
Interactive docs:
http://localhost:8000/docs
Tests use SQLite in-memory, so no database setup is required.
pytestcurl -X POST http://localhost:8000/orders/ \
-H "Content-Type: application/json" \
-d '{"item": "laptop"}'curl http://localhost:8000/orders/1curl http://localhost:8000/orders/This project uses a feature-based modular structure:
modules/orderscontains everything related to ordersrepository.py→ database accessservice.py→ business logicroutes.py→ API layer
This keeps the codebase:
- easy to navigate
- easy to extend
- ready to evolve into microservices if needed
- Order state transitions (finite state machine)
- Update / delete endpoints
- Authorization
- Integration tests with PostgreSQL
- Docker support for full deployment