# alikheza/fastapi_lu architecture

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

- Interactive diagram: https://gitdiagram.com/alikheza/fastapi_lu
- Repository: https://github.com/alikheza/fastapi_lu

## Overview

##### Project Type and Core Purpose
- This is a full-stack web application focused on student-related operations. It uses FastAPI as the web framework, PostgreSQL as the database, and SQLAlchemy ORM for database interactions.
- JWT tokens are used to secure endpoints with authentication.
- The project supports CRUD operations for various student-related entities such as admin, course, professor, and student.

##### Main Components and Their Roles
a. Application Layer (FastAPI Server):
- Located in the “app” directory.
- Contains the entry point (app/main.py) that starts the FastAPI server.
- Additional files like app/authentication.py, app/config.py, and app/dependency.py handle authentication, configuration, and dependency injection respectively.

b. Routing Layer:
- Found in the “routers” directory, with files for different user roles (admin, course, professor, student).
- Each router defines endpoints and maps incoming HTTP requests to corresponding controller logic.

c. Database Layer:
- The “database” directory holds scripts for configuring and interacting with the PostgreSQL database.
- database/models.py defines the ORM models.
- database/connect.py is likely responsible for establishing a connection to the database.
- The “database/CRUD” subdirectory holds modules that encapsulate Create, Read, Update, and Delete (CRUD) operations for different entities.

d. Schema Definitions:
- The “shcemas” directory (presumably intended to be “schemas”) contains definitions of data schemas for entities such as auth, course, professor, student, and user. These provide data validation and serialization for API requests and responses.

e. Infrastructure/Dockerization:
- Dockerfile and compose.yaml are provided for containerizing the application and handling multi-container orchestration (e.g., linking the application with the PostgreSQL database container).
- .env file configuration (as described in the README) plays a key role in setting environment variables for the application.

##### Relationships and Interactions
- The FastAPI application (app/main.py) serves as the central hub. It initializes the application, configures routes from the “routers” directory, and integrates the authentication logic.
- HTTP requests flow from clients to the server. Endpoints defined in the “routers” directory will call CRUD operations in the “database/CRUD” modules.
- The ORM models in database/models.py and SQLAlchemy handle persistence in the PostgreSQL database.
- The “shcemas” directory provides the models used to validate and serialize input/output data for each endpoint.
- JWT authentication tokens are generated in the authentication components and validated upon receiving the requests.
- Docker Compose (compose.yaml) and Dockerfile are responsible for initializing both the application container and the PostgreSQL container for ease of deployment.

##### Architectural Patterns and Design Principles
- Modularized structure: clear separation of concerns (e.g., business logic in the “database/CRUD” modules; routing in “routers” folder; schema definitions).
- Layered architecture: Presentation (routers), Business Logic (app and database CRUD), and Data Access (ORM models and database connection).
- Dependency injection is utilized in app/dependency.py, making the components easily testable and maintainable.
- JWT-based stateless authentication is implemented for securing sessions.

##### Technologies, Frameworks, and Libraries
- FastAPI – handles web requests and routing.
- SQLAlchemy – ORM for PostgreSQL database interactions.
- PostgreSQL – as the relational database.
- JWT – for secure token-based authentication.
- Docker – containerization and deployment (Dockerfile and compose.yaml).

- On the left, show “Clients/Users” making HTTP requests.
- FastAPI Application (from app/main.py)
- Routing Layer (routers directory: admin, course, professor, student)
- Authentication/Configuration Modules (app/authentication, app/config)
- In the middle/interacting component, “Business Logic Layer” that communicates with:
- CRUD modules (database/CRUD)
- Data Schemas (shcemas for request/response validation)
- On the right, the “Database Layer”:
- ORM Models (database/models.py) and connection (database/connect.py)
- Infrastructure symbols for Docker components representing container orchestration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Layer
    subgraph "Clients/Users"
        Clients["Clients/Users"]:::client
    end

    %% API Gateway / Server Layer
    subgraph "API Gateway / Server"
        FASTAPI["app/main.py – FastAPI Entry Point"]:::service
        Auth["app/authentication.py – JWT Authentication"]:::service
        Config["app/config.py – Configuration"]:::service
        Dependency["app/dependency.py – Dependency Injection"]:::service
        RouterA["routers/admin.py – Admin Router"]:::router
        RouterB["routers/course.py – Course Router"]:::router
        RouterC["routers/professor.py – Professor Router"]:::router
        RouterD["routers/student.py – Student Router"]:::router
        SWAGGER["/docs – Swagger Documentation"]:::note
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        CRUD["database/CRUD – CRUD Operations"]:::service
        Schemas["shcemas – Schema Definitions"]:::schema
    end

    %% Database Layer
    subgraph "Database Layer"
        DBModels["database/models.py – ORM Models"]:::db
        DBConnect["database/connect.py – DB Connection"]:::db
        Postgres["PostgreSQL Database"]:::dbTier
    end

    %% Infrastructure Layer
    subgraph "Infrastructure"
        Dockerfile["Dockerfile"]:::docker
        Compose["compose.yaml"]:::docker
        EnvFile[".env – Environment Config"]:::docker
    end

    %% Connections
    Clients -->|"sends requests"| FASTAPI
    Clients -->|"accesses"| SWAGGER

    FASTAPI -->|"uses"| Auth
    FASTAPI -->|"uses"| Config
    FASTAPI -->|"uses"| Dependency

    FASTAPI -->|"routes to"| RouterA
    FASTAPI -->|"routes to"| RouterB
    FASTAPI -->|"routes to"| RouterC
    FASTAPI -->|"routes to"| RouterD

    RouterA -->|"calls"| CRUD
    RouterB -->|"calls"| CRUD
    RouterC -->|"calls"| CRUD
    RouterD -->|"calls"| CRUD

    CRUD -->|"validates with"| Schemas

    CRUD -->|"persists data via"| DBModels
    CRUD -->|"connects through"| DBConnect

    DBModels -->|"stores into"| Postgres
    DBConnect -->|"manages connection to"| Postgres

    Dockerfile -->|"builds"| FASTAPI
    Compose -->|"orchestrates"| FASTAPI
    Compose -->|"orchestrates"| Postgres

    %% Click Events
    click FASTAPI "https://github.com/alikheza/fastapi_lu/blob/main/app/main.py"
    click Auth "https://github.com/alikheza/fastapi_lu/blob/main/app/authentication.py"
    click Config "https://github.com/alikheza/fastapi_lu/blob/main/app/config.py"
    click Dependency "https://github.com/alikheza/fastapi_lu/blob/main/app/dependency.py"
    click RouterA "https://github.com/alikheza/fastapi_lu/blob/main/routers/admin.py"
    click RouterB "https://github.com/alikheza/fastapi_lu/blob/main/routers/course.py"
    click RouterC "https://github.com/alikheza/fastapi_lu/blob/main/routers/professor.py"
    click RouterD "https://github.com/alikheza/fastapi_lu/blob/main/routers/student.py"
    click CRUD "https://github.com/alikheza/fastapi_lu/tree/main/database/CRUD/"
    click DBModels "https://github.com/alikheza/fastapi_lu/blob/main/database/models.py"
    click DBConnect "https://github.com/alikheza/fastapi_lu/blob/main/database/connect.py"
    click Schemas "https://github.com/alikheza/fastapi_lu/tree/main/shcemas/"
    click Dockerfile "https://github.com/alikheza/fastapi_lu/tree/main/Dockerfile"
    click Compose "https://github.com/alikheza/fastapi_lu/blob/main/compose.yaml"

    %% Styles
    classDef client fill:#FFECB3,stroke:#F57C00,stroke-width:2px;
    classDef service fill:#B3E5FC,stroke:#0288D1,stroke-width:2px;
    classDef router fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef schema fill:#E1BEE7,stroke:#8E24AA,stroke-width:2px;
    classDef db fill:#FFCDD2,stroke:#C62828,stroke-width:2px;
    classDef dbTier fill:#F0F4C3,stroke:#9E9D24,stroke-width:2px;
    classDef docker fill:#D1C4E9,stroke:#673AB7,stroke-width:2px;
    classDef note fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px;
```
