# sanjeev-thiyagarajan/fastapi-course architecture

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

- Interactive diagram: https://gitdiagram.com/sanjeev-thiyagarajan/fastapi-course
- Repository: https://github.com/sanjeev-thiyagarajan/fastapi-course

## Overview

##### Project Type and Purpose
- This is a RESTful backend service for a social media clone built with FastAPI.
- It provides 4 main API routes: posts, users, authentication, and voting.
- It uses PostgreSQL for persistence, Alembic for migrations, and runs in Docker (with nginx and Gunicorn in production).

##### File Structure
Top-level folders and files reveal the layers:
- app/
– routers/ (auth.py, post.py, user.py, vote.py) – API endpoints
– models.py (SQLAlchemy ORM models)
– schemas.py (Pydantic request/response schemas)
– database.py (DB session setup)
– oauth2.py (JWT token creation/validation)
– utils.py and calculations.py (business logic/helpers)
– config.py (env var loading)
– main.py (FastAPI app instantiation, include_router calls)
- alembic/ (DB migration scripts and env)
- Dockerfile, docker-compose-dev.yml/prod, nginx/, gunicorn.service – Deployment
- tests/ – automated test suite using pytest
- README.md – high-level overview and setup instructions

##### README for Additional Insights
- Confirms 4 route groups and PostgreSQL dependency.
- Mentions environment variables (.env) for DB credentials and JWT secret.
- Describes local dev vs. prod deployments.

##### Main Components & Their Relationships
a. API Gateway / Reverse Proxy Layer
- nginx container (in prod) receives HTTP traffic and forwards to Gunicorn
b. Application Servers
- Gunicorn master process with multiple Uvicorn worker processes
- Each worker runs the FastAPI application code (main.py)
c. FastAPI Application
- Routers (auth, post, user, vote)
- Controllers/business logic in utils.py, calculations.py
- oauth2.py for JWT
- Pydantic schemas for validation
d. Data Persistence
- PostgreSQL database (in its own container)
- SQLAlchemy ORM for CRUD operations (models.py + database.py session)
- Alembic for schema migrations
e. CI/CD & DevOps
- GitHub Actions workflow (build-deploy.yml)
- Docker Compose for local dev environment (fast reload)
- Dockerfile for image build, gunicorn.service for systemd in prod

##### Architectural Patterns & Design Principles
- Layered Architecture:
- Presentation (nginx reverse proxy)
- Application/API (FastAPI routers + controllers)
- Domain/Business Logic (calculations.py, utils.py)
- Data Access (SQLAlchemy + Alembic)
- Dependency Injection via FastAPI’s Depends
- Separation of Concerns: routers vs. schemas vs. models
- Twelve-factor app principles for config (env vars in config.py)

## Diagram (Mermaid)

```mermaid
graph TD
    client["Client"]:::external

    subgraph "Production Deployment"
        dockerProd["docker-compose-prod.yml"]:::infra
        nginx["nginx (Reverse Proxy)"]:::infra
        gunicorn["Gunicorn & Uvicorn Workers"]:::infra
        gunicornService["gunicorn.service"]:::infra
    end

    subgraph "Development Deployment"
        docker_dev["docker-compose-dev.yml\n(Live Reload)"]:::infra
    end

    client -->|"HTTP"| nginx
    nginx -->|"HTTP"| gunicorn
    docker_dev -->|"HTTP"| gunicorn
    dockerProd -->|"Deploy"| gunicorn

    gunicorn -->|"Invoke FastAPI App"| fastapi["FastAPI App"]:::app

    subgraph "Application Layer"
        fastapi 
        routers["Router Manager"]:::appGroup
        authR["Auth Router"]:::app
        postR["Post Router"]:::app
        userR["User Router"]:::app
        voteR["Vote Router"]:::app
        schemas["Schemas (Pydantic)"]:::app
        oauth2["OAuth2 Module"]:::app
        utils["Utils"]:::app
        calc["Calculations"]:::app
        config["Config Loader"]:::app
    end

    fastapi --> routers
    routers --> authR
    routers --> postR
    routers --> userR
    routers --> voteR
    routers --> schemas
    authR --> oauth2
    routers --> utils
    utils --> calc
    fastapi --> config

    subgraph "Data Layer"
        dbSession["DB Session Setup"]:::data
        models["Domain Models"]:::data
        postgres["PostgreSQL Database"]:::dataStore
    end

    routers -->|"DB Session"| dbSession
    dbSession --> models
    models -->|"SQL" | postgres

    subgraph "Migrations"
        alembicIni["alembic.ini"]:::migr
        alembicEnv["alembic/env.py"]:::migr
        scriptMako["alembic/script.py.mako"]:::migr
        versions["alembic/versions/*.py"]:::migr
    end

    alembicIni --> alembicEnv --> scriptMako --> versions --> postgres

    subgraph "CI/CD & Testing"
        gh["GitHub Actions"]:::ci
        tests["tests/"]:::test
    end

    gh --> docker_dev
    gh --> dockerProd
    tests --> fastapi

    %% Click Events
    click fastapi "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/main.py"
    click authR "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/routers/auth.py"
    click postR "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/routers/post.py"
    click userR "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/routers/user.py"
    click voteR "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/routers/vote.py"
    click models "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/models.py"
    click schemas "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/schemas.py"
    click dbSession "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/database.py"
    click oauth2 "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/oauth2.py"
    click utils "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/utils.py"
    click calc "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/calculations.py"
    click config "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/app/config.py"
    click alembicIni "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/alembic.ini"
    click alembicEnv "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/alembic/env.py"
    click scriptMako "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/alembic/script.py.mako"
    click versions "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/alembic/versions/*.py"
    click docker_dev "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/docker-compose-dev.yml"
    click dockerProd "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/docker-compose-prod.yml"
    click nginx "https://github.com/sanjeev-thiyagarajan/fastapi-course/tree/main/nginx/"
    click gunicornService "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/gunicorn.service"
    click gh "https://github.com/sanjeev-thiyagarajan/fastapi-course/blob/main/.github/workflows/build-deploy.yml"
    click tests "https://github.com/sanjeev-thiyagarajan/fastapi-course/tree/main/tests/"

    %% Styles
    classDef external fill:#AEDFF7,stroke:#0366d6,color:#0366d6;
    classDef infra fill:#B3D4FC,stroke:#004085,color:#004085;
    classDef app fill:#C8E6C9,stroke:#2E7D32,color:#2E7D32;
    classDef appGroup fill:#E8F5E9,stroke:#2E7D32,color:#2E7D32;
    classDef data fill:#FFE0B2,stroke:#EF6C00,color:#EF6C00;
    classDef dataStore fill:#FFCC80,stroke:#E65100,color:#E65100;
    classDef migr fill:#FFE7C3,stroke:#F57C00,color:#F57C00;
    classDef ci fill:#E1BEE7,stroke:#6A1B9A,color:#6A1B9A;
    classDef test fill:#F8BBD0,stroke:#C2185B,color:#C2185B;
```
