Skip to content

ummataliyev/fastapi-microservice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

120 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

fastapi-microservice

A production-ready FastAPI template with postgres, JWT auth, structured exceptions, and a worked auth/users example.

Stack

  • FastAPI 0.115 / Starlette
  • SQLAlchemy 2.0 (async) + asyncpg + Alembic
  • Pydantic 2 / pydantic-settings
  • PyJWT + passlib[bcrypt]
  • fastapi-pagination, prometheus-fastapi-instrumentator, sentry-sdk
  • APScheduler (lifespan-driven)
  • uv + ruff + pre-commit

Layout

src/
β”œβ”€β”€ api/                     # routers, dependencies, custom middlewares, exception handlers
β”œβ”€β”€ core/                    # settings, JWT config, postgres config, logging, timezone
β”œβ”€β”€ db/                      # async engine, session factory, mixins, declarative base
β”œβ”€β”€ enums/
β”œβ”€β”€ exceptions/
β”‚   β”œβ”€β”€ repositories/
β”‚   └── services/            # raised by services; auto-translated to JSON 4xx/5xx
β”œβ”€β”€ integrations/
β”œβ”€β”€ jobs/lifespan.py         # APScheduler entry; register cron jobs here
β”œβ”€β”€ managers/db/             # TransactionManager / ReadonlyManager β€” wire repos here
β”œβ”€β”€ mappers/                 # ORM ↔ Pydantic translation
β”œβ”€β”€ migrations/              # Alembic
β”œβ”€β”€ models/                  # SQLAlchemy ORM models
β”œβ”€β”€ repositories/            # CRUD on models, returns mapped entities
β”œβ”€β”€ schemas/                 # Pydantic request/response schemas
β”œβ”€β”€ security/                # JWT + bcrypt (interfaces + implementations)
β”œβ”€β”€ services/                # business logic (BaseService[TM])
└── tests/

Quickstart

git clone <fork>
cd fastapi-microservice

cp infra/.env.example infra/.env
# edit infra/.env with your local DB_* and JWT_SECRET_KEY

# bring up postgres
docker compose -f infra/docker-compose.yaml up -d postgres

uv sync
uv run alembic upgrade head
uv run uvicorn src.main:app --reload

Open http://localhost:8000/template/docs (basic auth: admin / admin β€” see Settings.docs_password).

Endpoints

  • POST /template/api/v1/auth/register β€” create a user, returns access + refresh tokens
  • POST /template/api/v1/auth/login β€” exchange credentials for tokens
  • POST /template/api/v1/auth/refresh β€” exchange a refresh token for a new pair
  • GET /template/api/v1/auth/me β€” JWT payload of the current user
  • GET /template/api/v1/users β€” paginated list (auth required)
  • GET /template/api/v1/users/{id}
  • POST /template/api/v1/users
  • PATCH /template/api/v1/users/{id}
  • DELETE /template/api/v1/users/{id}
  • GET /template/health
  • GET /template/ready
  • GET /template/metrics (Prometheus)

Production checklist

Before exposing this template publicly, change the defaults:

  • JWT_SECRET_KEY β€” generate with openssl rand -hex 32. The default change-me-in-prod is unsafe and PyJWT will warn.
  • CORS_ALLOW_ORIGINS β€” set to the explicit list of front-end origins (e.g. ["https://app.example.com"]). The default ["*"] is for local dev only.
  • DOCS_USERNAME / DOCS_PASSWORD β€” change or disable the basic-auth-protected /template/docs endpoint.
  • DB_PASSWORD β€” pick a strong password and inject via secrets manager, not infra/.env.
  • SENTRY_DSN β€” optional but recommended; sentry integration is wired in src/main.py.

Customizing for a new service

  1. Find/replace /template/ with /<your-service>/ across src/main.py and src/api/v1/__init__.py.
  2. Rename in pyproject.toml: [project] name, [tool.hatch.build.targets.wheel] packages (already ["src"]).
  3. Add domain models, schemas, mappers, repositories, services, routers β€” follow the users example.
  4. Wire new repositories in src/managers/db/transaction.py and readonly.py.
  5. Register your routers in src/api/v1/__init__.py.

Running tests

uv run pytest src/tests/ -v

License

MIT

About

🟒 FastAPI microservice template with async support, modular architecture, and ready-to-use Docker, testing, and CI workflows.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors