# jdglaser/fastapi-example-project architecture

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

- Interactive diagram: https://gitdiagram.com/jdglaser/fastapi-example-project
- Repository: https://github.com/jdglaser/fastapi-example-project

## Overview

##### Main Components
- API Server: The core FastAPI application hosted in src/main.py.
- Routes: Organized under src/routes (with separate routers for authentication, user, and item operations).
- Services: Located in src/services (e.g., auth_service.py and item_service.py) that contain business logic.
- Models: Defined under src/models, representing data schemas for items, users, tokens, etc.
- Database Layer: This is split across several folders:
- Database connection and setup (src/database/database.py)
- Repositories that encapsulate CRUD operations (src/database/repos)
- Database tables definitions (src/database/tables)
- Migration Infrastructure: Utilizes Alembic for database schema migrations (src/alembic and alembic.ini).
- Utility Functions: Helpers and logging utilities are in src/util.
- Tests: Full test coverage is provided in the tests folder.
- Supporting Infrastructure: Docker (Dockerfile, docker-compose.yml) and Pipenv for dependency management, along with shell scripts for migrations and app/database startup.

##### Relationships and Interactions
- Client calls the FastAPI endpoints exposed via routes (auth_router, item_router, user_router).
- FastAPI routes invoke services (e.g., auth_service, item_service) to perform business logic.
- Services interact with database repositories to perform data operations.
- The database repositories use underlying table definitions for ORM-level database transactions.
- Alembic scripts (migrations) maintain the evolution of database schema changes.
- Docker and docker-compose manage deployment, linking containerized services (the FastAPI app and the database).

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clear division between routes (handling HTTP requests), service layer (business logic), repository layer (database abstraction), and utility functions.
- Layered Architecture: Presentation (API routes), business logic (services), and data access (repositories and database tables).
- Use of Dependency Injection, error handling, logging, and modular tests to enhance maintainability.
- Containerization: Utilizes Docker for creating isolated environments for both the application and the database.

##### Relevant Technologies and Frameworks
- FastAPI: For asynchronous API serving.
- Uvicorn: ASGI server used to run the FastAPI app.
- Docker & Docker Compose: For containerization and running services.
- Alembic: For managing database migrations.
- Pipenv: For dependency management.
- Testing Frameworks: Likely Pytest, given the structure in the tests directory.

a. Client/User interacting with the “FastAPI Example Project” via HTTP requests.
- “Routes” (showing auth_router, item_router, user_router)
- “Services” (auth_service, item_service)
- “Models” (item_models, user_models, token_models)
e. A side branch for “Infrastructure & Deployment”:
- Docker files (Dockerfile, docker-compose.yml)
- Shell scripts for starting the app and database, migration generation, and cleanup.
- Dependency Management via Pipenv.
f. Lastly, point out “Documentation” (docs folder) and “Tests” (tests folder) as complementary parts of the overall project structure.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Client Layer"
        Client["Client (User)"]:::client
    end

    subgraph "API Layer"
        API["FastAPI Application (src/main.py)"]:::api

        subgraph "API Routes"
            AR1["Auth Routes"]:::routes
            AR2["Item Routes"]:::routes
            AR3["User Routes"]:::routes
        end

        subgraph "Business Services"
            S1["Auth Service"]:::services
            S2["Item Service"]:::services
        end

        subgraph "Data Models"
            M1["Item Models"]:::models
            M2["User Models"]:::models
            M3["Token Models"]:::models
            M4["Main Model"]:::models
        end
    end

    subgraph "Database Layer"
        DBConn["Database Connection (src/database/database.py)"]:::db
        
        subgraph "Repositories"
            R1["Base Repository"]:::db
            R2["Item Repository"]:::db
            R3["User Repository"]:::db
        end

        subgraph "Table Definitions"
            T1["Common Columns/Definitions"]:::db
            T2["Item Tables"]:::db
            T3["User Tables"]:::db
        end

        DBContainer["Database (Container)"]:::db
    end

    subgraph "Migration Infrastructure"
        MIG1["Alembic Configuration"]:::migrations
        MIG2["Alembic Scripts"]:::migrations
    end

    subgraph "Utility Functions"
        UTIL["Helpers & Logging (src/util)"]:::utils
    end

    subgraph "Tests"
        TESTS["Tests Directory"]:::tests
    end

    subgraph "Deployment & Infrastructure"
        D1["Dockerfile"]:::infra
        D2["docker-compose.yml"]:::infra
        SS["Shell Scripts"]:::infra
        PM1["Pipfile"]:::infra
        PM2["Pipfile.lock"]:::infra
    end

    Client -->|"HTTP_Request"| API
    API -->|"invokes"| AR1
    API -->|"invokes"| AR2
    API -->|"invokes"| AR3
    AR1 -->|"calls"| S1
    AR2 -->|"calls"| S2
    AR3 -->|"calls"| S1
    AR3 -->|"calls"| S2
    S1 -->|"accesses"| R1
    S1 -->|"accesses"| R2
    S2 -->|"accesses"| R3
    R1 --> DBConn
    R2 --> DBConn
    R3 --> DBConn
    DBConn -->|"DB_Operations"| DBContainer
    MIG1 -->|"migrates"| DBContainer
    MIG2 -->|"migrates"| DBContainer
    UTIL -.-> API
    TESTS -.-> API
    API -->|"logs"| UTIL
    API -->|"deployed_via"| D1
    API -->|"deployed_via"| D2
    DBContainer -->|"orchestrated_by"| D2
    D1 -->|"managed_by"| SS
    D2 -->|"managed_by"| SS
    API -->|"dependency"| PM1
    API -->|"dependency"| PM2

    click API "https://github.com/jdglaser/fastapi-example-project/blob/main/src/main.py"
    click AR1 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/routes/auth_router.py"
    click AR2 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/routes/item_router.py"
    click AR3 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/routes/user_router.py"
    click S1 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/services/auth_service.py"
    click S2 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/services/item_service.py"
    click M1 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/models/item_models.py"
    click M2 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/models/user_models.py"
    click M3 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/models/token_models.py"
    click M4 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/models/main_model.py"
    click DBConn "https://github.com/jdglaser/fastapi-example-project/blob/main/src/database/database.py"
    click R1 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/database/repos/base_repo.py"
    click R2 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/database/repos/item_repo.py"
    click R3 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/database/repos/user_repo.py"
    click T1 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/database/tables/common_columns.py"
    click T2 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/database/tables/item_tables.py"
    click T3 "https://github.com/jdglaser/fastapi-example-project/blob/main/src/database/tables/user_tables.py"
    click MIG1 "https://github.com/jdglaser/fastapi-example-project/blob/main/alembic.ini"
    click MIG2 "https://github.com/jdglaser/fastapi-example-project/tree/main/src/alembic"
    click UTIL "https://github.com/jdglaser/fastapi-example-project/tree/main/src/util"
    click TESTS "https://github.com/jdglaser/fastapi-example-project/tree/main/tests"
    click D1 "https://github.com/jdglaser/fastapi-example-project/tree/main/Dockerfile"
    click D2 "https://github.com/jdglaser/fastapi-example-project/blob/main/docker-compose.yml"
    click SS "https://github.com/jdglaser/fastapi-example-project/tree/main/scripts"
    click PM1 "https://github.com/jdglaser/fastapi-example-project/tree/main/Pipfile"
    click PM2 "https://github.com/jdglaser/fastapi-example-project/blob/main/Pipfile.lock"

    classDef client fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef api fill:#ccffcc,stroke:#008000,stroke-width:2px;
    classDef routes fill:#ffffcc,stroke:#999900,stroke-width:2px;
    classDef services fill:#cce5ff,stroke:#3366ff,stroke-width:2px;
    classDef models fill:#ffd9b3,stroke:#e68a00,stroke-width:2px;
    classDef db fill:#e6e6fa,stroke:#8a2be2,stroke-width:2px;
    classDef migrations fill:#ffdab9,stroke:#ff4500,stroke-width:2px;
    classDef utils fill:#f0e68c,stroke:#b8860b,stroke-width:2px;
    classDef tests fill:#ffe6e6,stroke:#ff6666,stroke-width:2px;
    classDef infra fill:#d3d3d3,stroke:#696969,stroke-width:2px;
```
