Skip to content

Metal-Bat/async-fast-api-base

Repository files navigation

Async FastAPI Sample (Modern Backend Template)

A production-ready FastAPI template designed for scalability, observability, and clean architecture.

It demonstrates a modern Python backend stack using:

  • Clean Architecture (DDD-inspired)
  • Docker-first development
  • Observability (OpenTelemetry + Jaeger)
  • Strong typing & linting (Ruff + Ty)
  • Modern tooling (uv + mise)
  • Fully automated dev workflow

✨ Features

  • ⚑ FastAPI (async-first API framework)
  • 🧱 Clean Architecture (domain / application / data / presentation)
  • 🐘 PostgreSQL (asyncpg + SQLAlchemy 2 + SQLModel)
  • πŸ” Alembic migrations
  • πŸ“¦ Dockerize full stack (backend + postgres + observability stack)
  • πŸ“Š OpenTelemetry tracing (FastAPI + collector + Jaeger)
  • 🧠 Strong typing (Ty)
  • 🧹 Ruff for linting + formatting
  • πŸ§ͺ Pytest + coverage + benchmarking
  • πŸ” Security scanning (bandit + detect-secrets)
  • 🧾 Conventional commits (Commitizen)
  • πŸš€ Developer experience via mise

πŸ—οΈ Architecture

This project follows a modular domain-driven structure:

src/
β”œβ”€β”€ apps/
β”‚   └── users/
β”‚       β”œβ”€β”€ domain/         # Entities + DTOs (pure business logic)
β”‚       β”œβ”€β”€ application/    # Use cases / services
β”‚       β”œβ”€β”€ data/           # Repositories / DB layer
β”‚       └── presentation/   # API routes (FastAPI)
β”‚
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ settings.py
β”‚   β”œβ”€β”€ base_service.py
β”‚   β”œβ”€β”€ base_repository.py
β”‚   └── deps.py
β”‚
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ logging_config.py
β”‚   β”œβ”€β”€ middleware.py
β”‚   β”œβ”€β”€ exceptions.py
β”‚   └── pagination.py
β”‚
β”œβ”€β”€ migrations/            # Alembic migrations
└── main.py

πŸš€ Quick Start

1. Install tooling

This project uses mise:

mise install

2. Setup environment

mise run setup

This will:

  • Install dependencies (uv)
  • Setup pre-commit hooks

3. Start infrastructure

mise run up

This starts:

  • Backend API
  • PostgreSQL
  • Observability stack (Jaeger + OpenTelemetry collector)

4. Run migrations

mise run migrate

5. Start development

mise run logs

or run backend shell:

mise run shell

πŸ“š API Documentation

Once backend is running:

Swagger UI

mise run swagger

πŸ‘‰ http://localhost:8000/docs


πŸ§ͺ Testing

Run all tests:

mise run test

Run with coverage:

mise run coverage

Output:

  • Terminal coverage report
  • HTML report β†’ htmlcov/index.html

πŸ“Š Benchmarking

Run performance benchmarks:

mise run benchmark

πŸ” Security

Run security checks:

mise run security

Includes:

  • Bandit (static security analysis)
  • Detect-secrets (secret scanning)

🧹 Code Quality

Format code

mise run fmt

Lint code

mise run lint

Fix lint issues

mise run lint-fix

Type check

mise run typecheck

Full check

mise run check

🐳 Docker Workflow

Start everything

mise run up

Stop everything

mise run down

Rebuild

mise run rebuild

Logs

mise run logs

Shell into backend

mise run shell

πŸ—„οΈ Database

Run migrations

mise run migrate

Create migration

mise run makemigrations -m "your message"

Check migrations

mise run db-current
mise run db-history

🧠 Observability

This project includes full distributed tracing:

  • OpenTelemetry SDK
  • FastAPI instrumentation
  • OTEL Collector
  • Jaeger UI

Access Jaeger:

πŸ‘‰ http://localhost:16686


🧾 Commit Workflow

This project enforces Conventional Commits.

Interactive commit

mise run commit

Version bump + changelog

mise run bump

πŸ§ͺ CI Pipeline (Local)

Simulate CI locally:

mise run ci

Runs:

  • formatting
  • linting
  • typing
  • tests
  • security checks

🧰 Tech Stack

Layer Tech
API FastAPI
DB PostgreSQL
ORM SQLAlchemy + SQLModel
Migrations Alembic
Observability OpenTelemetry + Jaeger
Validation Pydantic v2
Typing Ty
Linting Ruff
Testing Pytest
Runtime Uvicorn
Packaging uv
Dev UX mise

πŸ“¦ Project Philosophy

This template is built around:

  • Predictable structure
  • Zero hidden magic
  • Fast local iteration
  • Production parity with Docker
  • Strict typing + linting
  • Observability by default

πŸ§ͺ Example Endpoint Structure

users/
β”œβ”€β”€ domain/        β†’ pure logic
β”œβ”€β”€ application/   β†’ use cases
β”œβ”€β”€ data/          β†’ DB layer
└── presentation/  β†’ API routes

Each layer is independent β†’ easy testing, scaling, and refactoring.


🧼 Notes

  • No global dependencies (everything via uv)
  • No Node required
  • Fully reproducible environments
  • Designed for scaling into microservices

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors