# scholarly-insight/scholarly-insight-backend architecture

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

- Interactive diagram: https://gitdiagram.com/scholarly-insight/scholarly-insight-backend
- Repository: https://github.com/scholarly-insight/scholarly-insight-backend

## Overview

##### Main Components
- Backend Application: Represented by the FastAPI server (backend/app/main.py), which orchestrates the API endpoints.
- API Endpoints Layer: Located in backend/app/api/v1/endpoints. These routes handle incoming HTTP requests – for example, endpoints for authentication and user management.
- Core Services:
- Configuration (backend/app/core/config.py) – managing global settings.
- Database Connection (backend/app/core/database.py) – providing connectivity to PostgreSQL.
- Firebase Integration (backend/app/core/firebase.py) – handling Firebase authentication.
- Models & Schemas:
- Models (backend/app/models) – SQLAlchemy models that define the database schema.
- Schemas (backend/app/schemas) – Pydantic models that validate and serialize API request/response data.
- Business Logic / Services: (backend/app/services) – encapsulating the core business logic such as authentication and user management.
- Database and Migrations:
- PostgreSQL – as the primary data store.
- Alembic Migration Scripts (in backend/alembic and its subdirectories) – managing schema changes.

##### Relationships and Interactions
- The FastAPI Application (main.py) loads and configures the routing layers from the API directory.
- API endpoints call services (from backend/app/services) that use models (from backend/app/models) to query the PostgreSQL database.
- The database connection, configured in core/database.py, ensures a consistent and reliable link between the API and PostgreSQL.
- Firebase integration (via core/firebase.py) introduces external authentication into API endpoints. For example, “get_current_user” (from app/api/deps.py) is used to inject Firebase-authenticated users into protected routes.
- The configuration module (core/config.py) pulls necessary environmental variables as defined in the .env file.

##### Architectural Patterns & Important Design Principles
- Separation of Concerns: Distinct directories for API endpoints, core configurations, models, schemas, services, and migration files ensure modularity.
- Dependency Injection: Noticeable in how route handlers use dependencies (e.g., get_current_user) to enforce authentication.
- Database Migration Workflow: Using Alembic (with alembic.ini and the alembic directory) indicates a design that anticipates schema evolution, which is important for production-readiness.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["HTTP Client Request"]:::external
    B["FastAPI Server"]:::fastapi
    subgraph "API Layer"
        C["API Endpoints (v1)"]:::api
    end
    D["Business Logic / Services"]:::business
    subgraph "Core Modules"
        K["Configuration Module"]:::core
        E["Database Connection"]:::core
        I["Firebase Integration"]:::core
    end
    subgraph "Data Layer"
        F["SQLAlchemy Models"]:::data
        G["Schemas"]:::data
        H["PostgreSQL Database"]:::data
        J["Alembic Migration Scripts"]:::data
        L["Alembic Config"]:::data
    end

    A -->|"HTTPRequest"| B
    B -->|"LoadRoutes"| C
    C -->|"ProcessRequest"| D
    D -->|"UseDBConnection"| E
    D -->|"UtilizeModels"| F
    D -->|"ValidateData"| G
    E -->|"QueryUpdate"| H
    K -->|"ConfigureServer"| B
    K -->|"ProvideSettings"| E
    K -->|"ProvideFirebaseConfig"| I
    C -->|"InjectAuth"| I
    J -->|"MigrateSchema"| H
    L -->|"ConfigureDB"| H

    click B "https://github.com/scholarly-insight/scholarly-insight-backend/blob/main/backend/app/main.py"
    click C "https://github.com/scholarly-insight/scholarly-insight-backend/tree/main/backend/app/api/v1/endpoints"
    click K "https://github.com/scholarly-insight/scholarly-insight-backend/blob/main/backend/app/core/config.py"
    click E "https://github.com/scholarly-insight/scholarly-insight-backend/blob/main/backend/app/core/database.py"
    click I "https://github.com/scholarly-insight/scholarly-insight-backend/blob/main/backend/app/core/firebase.py"
    click F "https://github.com/scholarly-insight/scholarly-insight-backend/tree/main/backend/app/models"
    click G "https://github.com/scholarly-insight/scholarly-insight-backend/tree/main/backend/app/schemas"
    click D "https://github.com/scholarly-insight/scholarly-insight-backend/tree/main/backend/app/services"
    click J "https://github.com/scholarly-insight/scholarly-insight-backend/tree/main/backend/alembic"
    click L "https://github.com/scholarly-insight/scholarly-insight-backend/blob/main/backend/alembic.ini"

    classDef external fill:#ffd966,stroke:#333,stroke-width:2px;
    classDef fastapi fill:#a8d5e2,stroke:#333,stroke-width:2px;
    classDef api fill:#f9dcb4,stroke:#333,stroke-width:2px;
    classDef business fill:#f4a582,stroke:#333,stroke-width:2px;
    classDef core fill:#c7e9c0,stroke:#333,stroke-width:2px;
    classDef data fill:#d3d3f3,stroke:#333,stroke-width:2px;
```
