/fastapi-starter

FastAPI application bootstrap with app factory

Primary LanguagePythonMIT LicenseMIT

fastapi-starter

pre-commit

Quickly bootstrap pre-configured production grade FastAPI application.

FastAPI version corresponds to fastapi_starter package version.

This FastAPI project template is based on original tiangolo/full-stack-fastapi-postgresql

Features

  • Python 3.10

  • Pre-commit hooks for linting, formatting and testing - pre-commit

  • Logging Middleware for structlog and asgi-correlation-id.

  • Production deployment with gunicorn

  • Error reporting with Sentry

  • Isolated transactional database tests

  • HTTPSRedirectMiddleware; TrustedHostMiddleware; CORSMiddleware

  • Dependabot

  • Liveness and readiness endpoints

  • SQLAlchemy 2.0 style CRUD operations

Usage Example

  • Create app_factory.py on root level
from fastapi import FastAPI
from fastapi_starter import FastAPIStarterTemplate

from .api.api_v1.api import api_router as api_v1_router
from .core.config import Settings, get_settings


class ThisFastAPI(FastAPIStarterTemplate):
    def init_settings(self) -> Settings:
        return get_settings()

    def configure_default_routes(self) -> None:
        self.app.include_router(api_v1_router, prefix=self.settings.API_V1_STR)


def create_app() -> FastAPI:
    return ThisFastAPI().create_app()
  • Include default routes to your own api.api_v1.api
from fastapi_starter.api.api_v1.api import api_router

from .endpoints import login, scopes, users

api_router.include_router(login.router, prefix="/login", tags=["login"])
api_router.include_router(scopes.router, prefix="/scopes", tags=["scopes"])
api_router.include_router(users.router, prefix="/users", tags=["users"])
  • Extend base settings class in your own .core.config
from __future__ import annotations

from functools import lru_cache

from fastapi_starter.core.config import Settings as BaseSettings


@lru_cache
def get_settings() -> Settings:
    return Settings()


class Settings(BaseSettings):
    pass

Configuration

  • The application is configured with environment variables and will not start until all mandatory environment variables are passed.
  • Application configuration source is Settings class from src/fastapi_starter/core/config.py. It is a child class of pydantic.BaseSettings which allows configuration parameters to be automatically overridden by environment variables.
  • Environment variables and secrets for development should be set on the container, e.g. in docker-compose.yml.
  • For testing, settings dependency is overridden in test fixtures.
  • In production, environment variables are set on the Docker container itself.
  • With Pydantic secret support, environment variables can be loaded from files in /run/secrets, which is handy if you use Docker Secrets.

Installation

  • Required software:

  • Update global Python packages

python -m pip install -U pip wheel setuptools
  • Install dependencies
poetry install
  • Activate Python virtual environment
poetry shell
  • Install pre-commit hooks
pre-commit install

Development and testing

poetry run format/lint/test/...
  • Run all hooks at once
poetry run hooks
  • Export artifacts from Docker Image
poetry run export-test-results/export-dist