# rawheel/fastapi-boilerplate architecture

> Architecture diagram of the GitHub repository rawheel/fastapi-boilerplate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rawheel/fastapi-boilerplate
- Repository: https://github.com/rawheel/fastapi-boilerplate

## Overview

##### Project Type and Purpose
- This project is a FastAPI backend boilerplate designed for rapid setup of a production-ready CRUD application.
- It includes built-in support for database migrations (via Alembic), ORM (via SQLAlchemy), authentication, logging, and comprehensive testing (with Pytest).
- It utilizes Docker and docker-compose to provide containerized services including the FastAPI server, PostgreSQL database, and PgAdmin for database management.

##### File Structure
- Top-Level Components:
- “app” directory: Holds the main application code (initialization, routing, business logic, models, views, and schemas).
▸ app/main.py: The entry point for the FastAPI server.
▸ app/core: Core functionality such as authentication (auth.py), database sessions (both real and mock sessions), logging, and a central router.
▸ app/sneakers: Contains a domain-specific module (CRUD operations, models, schemas, views) demonstrating how APIs are organized.
▸ app/tests: Contains test cases, hinting at a Test-Driven Development (TDD) approach.
- “alembic” directory: For handling database migrations.
▸ Contains configuration (alembic.ini), environment setup, and version scripts for DB schema changes.
- Docker Setup:
▸ .docker and docker-compose.yml: Define the containerization of the application, Postgres, and PgAdmin.
- Other support files:
▸ README.md, pyproject.toml, and pre-commit configuration files ensure dependency management and code quality.

##### README for Additional Insights
- The README confirms that it’s a production-ready FastAPI backend with a structure similar to Django.
- It specifies that the solution comes ready with a docker-compose command to build up the services, and emphasizes integrations with PostgreSQL and PgAdmin.
- It outlines usage of specific libraries such as Alembic (for DB migrations), SQLAlchemy (for ORM), Pydantic (for data validation), and Loguru (for logging).

a. Main Components:
- API Server (FastAPI Application): Represented by “app/main.py” and components in “app/core” and “app/sneakers”. This is the central processing unit for client requests.
- Database:
▸ PostgreSQL: The main production database container.
▸ PgAdmin: A tool/service for managing the PostgreSQL database.
- Migration Service:
▸ Alembic: Manages database schema migrations.
- Containerization and Deployment:
▸ Docker & docker-compose: Wrap the different services (FastAPI app, PostgreSQL, PgAdmin) into isolated containers.
- Testing Framework and Environment:
▸ Test modules in “app/tests” indicate unit and possibly integration tests.
- Utility and Configuration:
▸ Configuration files (e.g., Dockerfile, .dockerignore, pyproject.toml) and pre-commit hooks ensure development quality.

b. Relationships and Interactions:
- The client (e.g., via Swagger UI on localhost:8000/docs) sends API requests that are processed by the FastAPI server.
- Business logic and data interactions are handled through defined routers and view functions found within the “app” modules.
- The FastAPI application interacts with the PostgreSQL database using SQLAlchemy sessions (real sessions in production; mock sessions for testing).
- Alembic ensures that any database schema changes are properly versioned and applied.
- Logging (via Loguru) provides central logging functionality that can be visualized as part of the requests’ lifecycle.
- Docker and docker-compose orchestrate the deployment and networking between these components.

c. Architectural Patterns and Design Principles:
- Clear separation of concerns:
▸ “app/core” handles application-wide services (authentication, logging, DB sessions).
▸ “app/sneakers” is a feature-specific module for CRUD operations, mirroring Django-like organization.
- Containerization for reproducibility and isolation.
- Testing as a first-class concept with separate configurations for production (PostgreSQL) vs. testing (SQLite or mock sessions).
- Use of industry-standard libraries (e.g., SQLAlchemy, Pydantic) follows best practices for ORM and data validation.

d. Relevant Technologies:
- FastAPI as the web framework.
- Docker and docker-compose for containerization.
- PostgreSQL and PgAdmin for database management.
- Alembic for handling database migrations.
- SQLAlchemy for ORM, with testing configurations for SQLite or mock sessions.
- Pytest for testing.
- Poetry for dependency management.
- Loguru for log handling.

##### Essential Elements
▸ Client ⇄ FastAPI Server (with authentication and API routing)
▸ FastAPI Server → Database (via SQLAlchemy sessions)
▸ Alembic → Database (for schema migrations)
▸ FastAPI Server → Logging (via Loguru)
▸ Application logic (e.g., business modules in “app/sneakers” and “app/core”)
▸ Infrastructure services (e.g., PostgreSQL, PgAdmin, Docker)
▸ Development aspects (e.g., testing and mock sessions)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Client (Swagger UI)"]:::appLayer

    %% FastAPI Application Layer
    subgraph "FastAPI Application"
        FS["FastAPI Server"]:::appLayer
        subgraph "Core Services"
            Auth["Authentication"]:::appLayer
            Router["Main Router"]:::appLayer
            DBSProd["DB Session (Production)"]:::appLayer
            DBSMock["DB Session (Mock)"]:::appLayer
            LogUtil["Logging Utility"]:::appLayer
        end
        subgraph "Domain Module (Sneakers)"
            CRUD["CRUD Operations"]:::appLayer
        end
    end

    %% Infrastructure: Database Services
    subgraph "Database Services"
        DB["PostgreSQL Database"]:::infra
        PgA["PgAdmin"]:::infra
    end

    %% Migration Manager (Alembic Service)
    subgraph "Alembic Service"
        AlembicDir["Alembic Directory"]:::migration
        AlembicCfg["Alembic Config"]:::migration
    end

    %% Containerization (Docker Environment)
    subgraph "Docker Environment"
        DockerDir["Docker Directory"]:::docker
        DockerIgnore[".dockerignore"]:::docker
        DockerCompose["Docker Compose"]:::docker
    end

    %% Testing Framework
    TestSuite["Test Suite"]:::test

    %% Connections
    Client -- "HTTP" --> FS
    FS -- "SQLAlchemy" --> DB
    AlembicDir -- "migrate" --> DB
    AlembicCfg -- "migrate" --> DB
    FS -- "Loguru" --> LogUtil
    DockerCompose -- "orchestrates" --> FS
    DockerCompose -- "orchestrates" --> DB
    DockerCompose -- "orchestrates" --> PgA
    TestSuite -- "tests" --> FS

    %% Click Events
    click FS "https://github.com/rawheel/fastapi-boilerplate/blob/main/app/main.py"
    click Auth "https://github.com/rawheel/fastapi-boilerplate/blob/main/app/core/auth.py"
    click Router "https://github.com/rawheel/fastapi-boilerplate/blob/main/app/core/main_router.py"
    click DBSProd "https://github.com/rawheel/fastapi-boilerplate/blob/main/app/core/db/session.py"
    click DBSMock "https://github.com/rawheel/fastapi-boilerplate/blob/main/app/core/db/mock_session.py"
    click LogUtil "https://github.com/rawheel/fastapi-boilerplate/blob/main/app/core/logger.py"
    click CRUD "https://github.com/rawheel/fastapi-boilerplate/tree/main/app/sneakers"
    click TestSuite "https://github.com/rawheel/fastapi-boilerplate/tree/main/app/tests"
    click AlembicDir "https://github.com/rawheel/fastapi-boilerplate/tree/main/alembic"
    click AlembicCfg "https://github.com/rawheel/fastapi-boilerplate/blob/main/alembic.ini"
    click DockerDir "https://github.com/rawheel/fastapi-boilerplate/blob/main/.docker"
    click DockerIgnore "https://github.com/rawheel/fastapi-boilerplate/blob/main/.dockerignore"
    click DockerCompose "https://github.com/rawheel/fastapi-boilerplate/blob/main/docker-compose.yml"

    %% Styles
    classDef appLayer fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef infra fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef migration fill:#FFA07A,stroke:#000,stroke-width:1px;
    classDef docker fill:#EEE8AA,stroke:#000,stroke-width:1px;
    classDef test fill:#FFFF99,stroke:#000,stroke-width:1px;
```
