⚑ Sangsakala Proxy 🏠 Home
Skip to content
Public template

Latest commit

Β 

History

101 Commits

Folders and files

Repository files navigation

FastAPI Project Template

A modern, production-ready FastAPI template with built-in features for rapid development and deployment.

Features & Tech Stack

Core Framework

  • FastAPI - Modern, fast web framework for building APIs with automatic OpenAPI documentation
  • FastAPI Pundra - Productivity companion for FastAPI (global exception handling, OpenAPI schema discovery, auto_bind_router for API modules)
  • Auto Route Discovery - Routers under app.api are discovered and mounted automatically

Database & ORM

  • PostgreSQL - Default target database (connection built from settings; psycopg2-binary driver)
  • SQLAlchemy - SQL toolkit and ORM
  • Alembic - Database migration management and version control

Configuration & Validation

  • Pydantic - Request/response models and validation
  • Pydantic Settings - Typed configuration from environment variables (app/config/settings.py)
  • Schema Layer - Organized schemas under app.schemas with OpenAPI discovery
  • Custom Serializers - Flexible data transformation under app/serializers

Task Management

  • TaskIQ - Async task queue for background jobs
  • taskiq-redis - Redis broker for TaskIQ
  • Task Scheduling - Sample scheduled tasks and scheduler scripts under scripts/

Security & Authentication

  • JWT Authentication - Bearer tokens via fastapi-pundra JWT utilities
  • Authorization Middleware - Validates JWT on protected routes; public routes are configured in EXCLUDE_PATHS (app/config/authorization.py)
  • Path Rules - Prefix, segment-wildcard, and exact path matching for public routes (app/lib/auth_path_utils.py)

HTTP & Integrations

  • HTTPX - Async-friendly HTTP client; shared helpers in app/lib/httpx_client.py

Development & Tools

  • UV - Python package manager and lockfile (uv.lock)
  • Pytest - Tests with pytest-asyncio, pytest-cov, factories (factory-boy / faker), and app/tests/conftest.py
  • Ruff - Linter/formatter (ruff.toml; also run via pre-commit)
  • Pre-commit - Git hooks (install with uv run pre-commit-install or use uv run lint to run hooks on all files)
  • Docker - Sample app Dockerfile and supervisor config under docker/

Additional Features

  • Email - fastapi-mail, Jinja2 templates, and premailer/inline CSS under app/templates/mails/
  • SQL File Management - Raw SQL files under app/sql_files/
  • Structured Logging - Loguru-based logging (app/utils/logger.py)

Prerequisites

  • Python 3.12 or higher
  • UV package manager (recommended)
  • PostgreSQL (or adjust db_* settings in .env to match your database)

Quick Start

1. Setup Project

Clone the repository and install dependencies:

# Install dependencies
uv sync

# Install with development dependencies (recommended for development)
uv sync --extra dev

Note: The --extra dev flag installs testing tools, Ruff, pre-commit, and related dev dependencies.

2. Environment Configuration

Create your environment configuration:

# Copy the example environment file
cp .env.example .env

# Edit .env: database host/user/password/name, SSL mode, project port, test DB URL, etc.
# Settings are loaded in app/config/settings.py (Pydantic Settings).

3. Database Setup

This project uses Alembic for database schema management and migrations.

Initialize and upgrade your database:

# Run database migrations
uv run db-upgrade

# Create a new database revision (message in quotes)
uv run db-revision "Initial database setup"

4. Run the Application

Start the development server:

# Development: auto-reload (port from settings, default 8000)
uv run start-server-dev

# Production-style: no reload
uv run start-server

The API will be available at (host/port depend on your settings; defaults shown):

5. Optional: Git hooks & tests

# Install pre-commit hooks (once per clone)
uv run pre-commit-install

# Run the full lint pipeline (pre-commit on all files)
uv run lint

# Tests with coverage (project script enforces coverage thresholds)
bash scripts/test.sh

# Or run pytest directly (see pyproject.toml for default pytest options)
uv run pytest

Project CLI entry points

Defined in pyproject.toml under [project.scripts]:

Command Purpose
uv run start-server Run the app with Uvicorn (no reload)
uv run start-server-dev Run with reload
uv run db-upgrade alembic upgrade head
uv run db-revision "message" alembic revision --autogenerate
uv run lint Run pre-commit on all files
uv run pre-commit-install pre-commit install

Directory structure

β”œβ”€β”€ _docs/                          # Extra documentation (see below)
β”œβ”€β”€ alembic/                        # Alembic env and migration versions
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/                        # HTTP routes (auto-bound from app.api)
β”‚   β”‚   β”œβ”€β”€ v1/
β”‚   β”‚   β”‚   β”œβ”€β”€ task_schedule_sample.py
β”‚   β”‚   β”‚   └── user.py
β”‚   β”‚   β”œβ”€β”€ health.py
β”‚   β”‚   β”œβ”€β”€ root_index.py
β”‚   β”‚   └── router.py
β”‚   β”œβ”€β”€ config/                     # Settings, CORS, authorization lists
β”‚   β”‚   β”œβ”€β”€ authorization.py       # EXCLUDE_PATHS (JWT not required)
β”‚   β”‚   β”œβ”€β”€ cors.py
β”‚   β”‚   └── settings.py            # Pydantic Settings / env
β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   β”œβ”€β”€ auth_path_utils.py     # Public-path matching for middleware
β”‚   β”‚   β”œβ”€β”€ database.py            # Engine, session, Base
β”‚   β”‚   β”œβ”€β”€ dot_env_loader.py
β”‚   β”‚   β”œβ”€β”€ httpx_client.py
β”‚   β”‚   └── tskq/                  # TaskIQ broker, scheduler helpers, invoker
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── authorization_middleware.py
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── users.py
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   └── user_schema.py
β”‚   β”œβ”€β”€ serializers/
β”‚   β”‚   └── user_serializer.py
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ scheduler_service.py
β”‚   β”‚   └── user_service.py
β”‚   β”œβ”€β”€ sql_files/users/           # Example raw SQL
β”‚   β”œβ”€β”€ tasks/                     # TaskIQ task modules
β”‚   β”œβ”€β”€ templates/                 # Jinja (e.g. mail HTML)
β”‚   β”œβ”€β”€ tests/                     # pytest: unit/, integration/, factories/, fixtures/
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ base.py
β”‚   β”‚   └── logger.py              # Loguru setup
β”‚   β”œβ”€β”€ cli.py                     # db-upgrade, db-revision, lint, pre-commit-install
β”‚   β”œβ”€β”€ main.py                    # FastAPI app factory
β”‚   └── taskiq.py                  # TaskIQ app/broker wiring
β”œβ”€β”€ docker/
β”‚   β”œβ”€β”€ config/supervisor/
β”‚   └── dockerfiles/
β”œβ”€β”€ scripts/                       # test.sh, lint.sh, format.sh, taskiq-*.sh, deploy.sh, etc.
β”œβ”€β”€ .coveragerc
β”œβ”€β”€ .pre-commit-config.yaml
β”œβ”€β”€ alembic.ini
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ ruff.toml
β”œβ”€β”€ uv.lock
└── README.md

Note

This project requires Python 3.12 or higher.

Postman collection documentation

Docs

Used by

Contributors

Languages