# faezehghiasi/fastapi-project architecture

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

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

## Overview

##### Project Type and Purpose
- This is a backend web‐service project (“Blog API”) built with FastAPI for HTTP endpoints, SQLAlchemy for ORM, and SQLite as the database.
- It provides user registration/authentication (OAuth2 + JWT) and CRUD operations on blog posts linked to users.

##### File Structure Analysis
- Top‐Level Files:
- README.md – Project overview, tech stack, API spec
- requirements.txt – Dependency list
- “blog/” Package:
- main.py – FastAPI app instantiation and router inclusion
- database.py – SQLAlchemy engine/session setup for SQLite
- models.py – ORM models (User, Blog)
- schemas.py – Pydantic schemas for request/response validation
- hashing.py – Password hashing utilities
- oauth2.py & token.py – JWT generation/validation, token settings
- repository/… – Data‐access layer with methods to CRUD users/blogs
- routers/… – Route handlers divided by domain (authentication, blogs, users)

##### Key Architectural Patterns
- Layered Architecture:
1. Presentation/API Layer (FastAPI routers)
2. Service/Business Layer (authentication, hashing, token logic)
3. Data Access Layer (repository classes using SQLAlchemy sessions)
4. Persistence Layer (SQLite database)
- Separation of Concerns: routers only handle HTTP; repositories only handle DB; models and schemas separate definitions.

##### Main Components
1. Client (e.g., browser, Postman, or mobile app)
2. FastAPI App
- Router Modules: authentication, user, blog
- Dependency Injection (DB session, OAuth2)
3. Authentication Service
- hashing.py (password hashing)
- oauth2.py/token.py (JWT creation & verification)
4. Repository Layer
- user.py & blog.py in blog/repository
- Exposes create, read, update, delete methods
5. Database
- SQLite DB file
- SQLAlchemy Engine + Session

Optional: External Clients (e.g., third‐party OAuth providers if extended)

##### Relationships & Data Flow
- Client → HTTP request (POST/GET/PUT/DELETE) → FastAPI Router
- Router uses Pydantic Schema to validate input → passes data to Service or Repository
- For protected routes: Router invokes OAuth2 dependency → verifies JWT → extracts user ID
- Repository layer uses SQLAlchemy session to query/commit data → returns model instances
- Router returns Pydantic Schema‐serialized response to Client

- Callouts for key technologies (FastAPI, SQLAlchemy, JWT, SQLite)
- Notes on protected routes and token validation

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    Client["Client<br/>(Browser/Postman)"]:::external

    %% Presentation/API Layer
    subgraph "Presentation/API Layer"
        direction TB
        FastAPIApp["FastAPI App"]:::api
        AuthRouter["authentication.py<br/>(Auth Routes)"]:::api
        UserRouter["user.py<br/>(User Routes)"]:::api
        BlogRouter["blogs.py<br/>(Blog Routes)"]:::api
        Schemas["schemas.py<br/>(Pydantic Schemas)"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        Hashing["hashing.py<br/>(Password Hashing)"]:::service
        OAuth2["oauth2.py<br/>(JWT Service)"]:::service
        TokenCfg["token.py<br/>(Token Config)"]:::service
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        direction TB
        UserRepo["user.py<br/>(User Repository)"]:::dao
        BlogRepo["blog.py<br/>(Blog Repository)"]:::dao
        Models["models.py<br/>(ORM Models)"]:::dao
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        direction TB
        DatabaseSetup["database.py<br/>(SQLAlchemy Engine/Session)"]:::db
        SQLiteDB["SQLite DB"]:::db
    end

    %% Documentation
    subgraph "Project Docs"
        direction TB
        README["README.md"]:::doc
        Requirements["requirements.txt"]:::doc
    end

    %% Relationships
    Client -->|"HTTP Request<br/>(POST/GET/PUT/DELETE)"| FastAPIApp
    FastAPIApp --> AuthRouter
    FastAPIApp --> UserRouter
    FastAPIApp --> BlogRouter

    AuthRouter -->|"validates input"| Schemas
    UserRouter -->|"validates input"| Schemas
    BlogRouter -->|"validates input"| Schemas

    AuthRouter -->|"hashes/verifies pwd"| Hashing
    AuthRouter -->|"generates/verifies JWT"| OAuth2
    AuthRouter -->|"uses settings"| TokenCfg

    UserRouter -->|"calls CRUD ops"| UserRepo
    BlogRouter -->|"calls CRUD ops"| BlogRepo

    UserRepo -->|"uses models & session"| Models
    BlogRepo -->|"uses models & session"| Models

    UserRepo --> DatabaseSetup
    BlogRepo --> DatabaseSetup

    DatabaseSetup --> SQLiteDB

    FastAPIApp --> README
    FastAPIApp --> Requirements

    %% Click Events
    click FastAPIApp "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/main.py"
    click DatabaseSetup "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/database.py"
    click Models "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/models.py"
    click Schemas "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/schemas.py"
    click Hashing "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/hashing.py"
    click OAuth2 "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/oauth2.py"
    click TokenCfg "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/token.py"
    click UserRepo "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/repository/user.py"
    click BlogRepo "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/repository/blog.py"
    click AuthRouter "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/routers/authentication.py"
    click UserRouter "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/routers/user.py"
    click BlogRouter "https://github.com/faezehghiasi/fastapi-project/blob/main/blog/routers/blogs.py"
    click README "https://github.com/faezehghiasi/fastapi-project/blob/main/README.md"
    click Requirements "https://github.com/faezehghiasi/fastapi-project/blob/main/requirements.txt"

    %% Styles
    classDef api fill:#D6E9FF,stroke:#225588,color:#000
    classDef service fill:#E2F7E1,stroke:#2B8F2B,color:#000
    classDef dao fill:#FFE6CC,stroke:#CB7A12,color:#000
    classDef db fill:#F0F0F0,stroke:#888888,color:#000
    classDef doc fill:#FDE2E4,stroke:#CC3366,color:#000
    classDef external fill:#E8E8E8,stroke:#555555,color:#000
```
