# ivanshepeta/fast_api_postproject architecture

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

- Interactive diagram: https://gitdiagram.com/ivanshepeta/fast_api_postproject
- Repository: https://github.com/ivanshepeta/fast_api_postproject

## Overview

##### Project type and purpose
- This is a backend-only RESTful API service built with FastAPI.
- Purpose: Provide secure endpoints for user registration, login (JWT-based), post management (CRUD), and file upload/streaming, all backed by a PostgreSQL database with Pydantic/SQLAlchemy for validation and ORM.

##### File structure
- Top level:
- app/ – contains all source code
- config, database connection, models, schemas, utils, OAuth2 logic, and main entrypoint
- app/routers/ – groups endpoint handlers by domain (auth, user, post)
- app/models.py – SQLAlchemy ORM definitions
- app/schemas.py – Pydantic request/response models
- app/oauth2.py – JWT token creation/verification and security dependencies
- app/database.py – sessionLocal + engine setup for PostgreSQL
- app/utils.py – common helper functions (e.g., password hashing)
- app/main.py – FastAPI instantiation, router mounts, middleware
- app/config.py – environment variable loading via Python-dotenv

##### README for additional insights
- Lists core technologies: FastAPI, PostgreSQL, SQLAlchemy, PyJWT, Pydantic
- Highlights JWT authentication, CRUD for posts, file upload/streaming

##### Components
a. Main components:
- Client (web/mobile frontend, API consumer)
- FastAPI Application
– Routers (Auth, User, Post)
– Dependencies (OAuth2, utils)
– Schemas & Validation
- Business Logic / Services (optional layer within utils or routers)
- Database Layer
– SQLAlchemy ORM + PostgreSQL
- File Storage/Streaming Subsystem (could be local disk or cloud bucket)
- External Services (e.g., email service, if any; not in this version)
b. Relationships and interactions:
1. Client → FastAPI (HTTP request)
2. FastAPI Router authenticates via OAuth2 dependency → PyJWT
3. Router invokes business logic (utils + schemas) to validate/fill data
4. Business logic uses SQLAlchemy session (app.database) → PostgreSQL
5. For file endpoints: FastAPI reads/writes to file storage and streams to client
6. FastAPI responds over HTTP back to client
c. Architectural patterns / design principles:
- Layered architecture: Controllers (routers) → Services (utils) → Data access (models + database)
- Dependency injection via FastAPI’s Depends (for DB sessions, OAuth2 scopes)
- Separation of concerns: Pydantic schemas vs. SQLAlchemy models
- Configuration abstraction via app.config (dotenv)
d. Relevant technologies/frameworks:
- FastAPI (ASGI server, routing, dependency injection)
- Pydantic (request/response validation)
- SQLAlchemy ORM (models, sessions)
- PyJWT (token encoding/decoding)
- Python-dotenv (environment management)
- PostgreSQL (persistent storage)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Client["API Consumer\n(Web/Mobile)"]:::external

    %% FastAPI Application Boundary
    subgraph "FastAPI Application" 
        direction TB
        Main["FastAPI App\n(app/main.py)"]:::compute
        Config["Config Loader\n(app/config.py)"]:::config
        DatabaseLayer["DB Session & Engine\n(app/database.py)"]:::storage
        Models["ORM Models\n(app/models.py)"]:::storage
        Schemas["Pydantic Schemas\n(app/schemas.py)"]:::validation
        OAuth2["OAuth2 Security\n(app/oauth2.py)"]:::security
        Utils["Utility Functions\n(app/utils.py)"]:::util

        subgraph "Routers (Controllers)"
            direction TB
            AuthRouter["Auth Router\n(app/routers/auth.py)"]:::compute
            UserRouter["User Router\n(app/routers/user.py)"]:::compute
            PostRouter["Post Router\n(app/routers/post.py)"]:::compute
        end
    end

    %% External Storage
    DB["PostgreSQL Database"]:::storage
    FileStore["File Storage\n(Local Disk/Cloud)"]:::storage

    %% Flows
    Client -->|"HTTP Request"| Main
    Main -->|"loads env"| Config
    Main -->|"mounts routers"| AuthRouter
    Main -->|"mounts routers"| UserRouter
    Main -->|"mounts routers"| PostRouter

    %% Router dependencies
    AuthRouter -->|"Depends on"| OAuth2
    UserRouter -->|"Depends on"| OAuth2
    PostRouter -->|"Depends on"| OAuth2

    %% Validation & Business Logic
    AuthRouter -->|"validate/request & response"| Schemas
    UserRouter -->|"validate/request & response"| Schemas
    PostRouter -->|"validate/request & response"| Schemas

    AuthRouter -->|"helper funcs"| Utils
    UserRouter -->|"helper funcs"| Utils
    PostRouter -->|"helper funcs"| Utils

    %% Data Access
    AuthRouter -->|"DB session"| DatabaseLayer
    UserRouter -->|"DB session"| DatabaseLayer
    PostRouter -->|"DB session"| DatabaseLayer

    DatabaseLayer -->|"reads/writes ORM"| Models
    DatabaseLayer -->|"SQL via ORM"| DB

    %% File operations
    PostRouter -->|"upload/download stream"| FileStore
    FileStore -->|"streams file"| Client

    %% Click Events
    click Main "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/main.py"
    click Config "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/config.py"
    click DatabaseLayer "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/database.py"
    click Models "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/models.py"
    click Schemas "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/schemas.py"
    click OAuth2 "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/oauth2.py"
    click Utils "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/utils.py"
    click AuthRouter "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/routers/auth.py"
    click UserRouter "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/routers/user.py"
    click PostRouter "https://github.com/ivanshepeta/fast_api_postproject/blob/master/app/routers/post.py"

    %% Styles
    classDef compute fill:#AEDFF7,stroke:#0288D1,color:#000
    classDef storage fill:#E3F2FD,stroke:#1976D2,color:#000
    classDef external fill:#FFF3E0,stroke:#FB8C00,color:#000
    classDef config fill:#E8F5E9,stroke:#43A047,color:#000
    classDef validation fill:#F3E5F5,stroke:#8E24AA,color:#000
    classDef security fill:#FFEBEE,stroke:#E53935,color:#000
    classDef util fill:#FFFDE7,stroke:#FBC02D,color:#000
```
