# isaaclopezvidal/qusar-api architecture

> Architecture diagram of the GitHub repository isaaclopezvidal/qusar-api, 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/isaaclopezvidal/qusar-api
- Repository: https://github.com/isaaclopezvidal/qusar-api

## Overview

##### Main Components
- API Endpoints / Cloud Functions: Represent the two public endpoints (topsecret and topsecret_split). These are the entry points into the system and are deployed on Google Cloud Functions.
- Entry Point & Server Setup: Identify the main file (app.js) which wires the system together, including loading routes and middlewares.
- Configuration Layer: Show the config directory where the database (config/db.js) and logger (config/logger.js) configurations are initialized.
- Middleware: Include the middleware directories (errors, topSecret, and topSecretSplit) where global error handling, authentication/authorization (if applicable), and request validations are performed.
- Route Handling: Represent the routes (routes.js file that likely imports from routes/topsecretRoute.js and routes/topsecretSplitRoute.js) that define how incoming HTTP requests are processed.
- Business Logic Layer (Services): Show the services component (services/location.js and services/message.js) where core functionalities such as location processing or messaging are implemented.
- External Dependencies: Include external services like Cloud SQL (MySQL) and, optionally, the Google Cloud Platform elements (Cloud Functions, Cloud Source Repositories, Cloud SDK) that are part of the deployment process.

##### Relationships & Interactions
- API requests first hit the Cloud Functions endpoints, then are routed by the central app (app.js) which invokes the appropriate route.
- Routes communicate with middleware layers which perform tasks such as error handling, input validation, and possibly authentication.
- After processing, routes call the services layer where the main business logic is executed.
- Services interact with the models to read from or write to the MySQL database.
- Configuration settings manage the connections and operational parameters across the entire stack (both local and cloud deployment scenarios).

##### Architectural Patterns & Principles
- Layered Architecture: The project clearly separates concerns by dividing the application into configuration, routing, middleware, business logic (services), and data models.
- Dependency Injection and Middleware Chaining: Middleware components handle common tasks (validation, error handling) before passing control to the specific business logic.
- Cloud-Native Design: Leveraging Google Cloud Functions for deployment shows a serverless approach, with external configuration for database access via Cloud SQL.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Cloud Functions"
        CF1["Topsecret Endpoint"]:::cloud
        CF2["Topsecret_split Endpoint"]:::cloud
    end

    C["API Server: app.js"]:::api

    subgraph "Configuration"
        Cfg1["DB Config"]:::config
        Cfg2["Logger Config"]:::config
    end

    subgraph "Routing Layer"
        D["Central Routes: routes.js"]:::route
        subgraph "Detailed Routes"
            R1["Topsecret Route"]:::route
            R2["Topsecret_split Route"]:::route
        end
    end

    subgraph "Middleware Layer"
        E["Global Error Handling"]:::middleware
        subgraph "TopSecret Middleware Chain"
            M1["topsecretMiddleware"]:::middleware
            M2["helperCatch"]:::middleware
            M3["topsecretValidation"]:::middleware
        end
        M4["TopSecretSplit Validation"]:::middleware
    end

    subgraph "Services Layer"
        S1["Location Service"]:::service
        S2["Message Service"]:::service
    end

    subgraph "Data Models"
        DM1["AstroNaves"]:::model
        DM2["Nave"]:::model
        DM3["Posicion"]:::model
        DM4["Satelite"]:::model
        DM5["Satelites"]:::model
        DM6["SatelitesView"]:::model
    end

    DB["MySQL / Cloud SQL"]:::database

    Ext["Google Cloud Platform Tools & Cloud SDK"]:::external

    %% Connections
    CF1 -->|"routes_to"| C
    CF2 -->|"routes_to"| C

    C -->|"loads"| D

    D -->|"distributes"| R1
    D -->|"distributes"| R2

    R1 -->|"processes"| M1
    M1 -->|"calls"| M2
    M2 -->|"verifies"| M3
    R2 -->|"processes"| M4

    M3 --> E
    M4 --> E

    E -->|"invokes"| S1
    E -->|"invokes"| S2

    S1 -->|"interacts_with"| DM1
    S1 -->|"interacts_with"| DM2
    S1 -->|"interacts_with"| DM3
    S2 -->|"interacts_with"| DM4
    S2 -->|"interacts_with"| DM5
    S2 -->|"interacts_with"| DM6

    DM1 -->|"reads/writes"| DB
    DM2 -->|"reads/writes"| DB
    DM3 -->|"reads/writes"| DB
    DM4 -->|"reads/writes"| DB
    DM5 -->|"reads/writes"| DB
    DM6 -->|"reads/writes"| DB

    %% Configuration influences
    Cfg1 -.-> C
    Cfg1 -.-> DB
    Cfg2 -.-> C
    Cfg2 -.-> E

    %% External tools influence
    Ext -.-> CF1
    Ext -.-> CF2
    Ext -.-> DB

    %% Click Events
    click CF1 "https://github.com/isaaclopezvidal/qusar-api/blob/master/routes/topsecretRoute.js"
    click CF2 "https://github.com/isaaclopezvidal/qusar-api/blob/master/routes/topsecretSplitRoute.js"
    click C "https://github.com/isaaclopezvidal/qusar-api/blob/master/app.js"
    click Cfg1 "https://github.com/isaaclopezvidal/qusar-api/blob/master/config/db.js"
    click Cfg2 "https://github.com/isaaclopezvidal/qusar-api/blob/master/config/logger.js"
    click E "https://github.com/isaaclopezvidal/qusar-api/blob/master/middleware/errors.js"
    click M1 "https://github.com/isaaclopezvidal/qusar-api/blob/master/middleware/topSecret/topsecretMiddleware.js"
    click M2 "https://github.com/isaaclopezvidal/qusar-api/blob/master/middleware/topSecret/helperCatch.js"
    click M3 "https://github.com/isaaclopezvidal/qusar-api/blob/master/middleware/topSecret/validationSchema.js"
    click M4 "https://github.com/isaaclopezvidal/qusar-api/blob/master/middleware/topSecretSplit/validationSchema.js"
    click D "https://github.com/isaaclopezvidal/qusar-api/blob/master/routes.js"
    click S1 "https://github.com/isaaclopezvidal/qusar-api/blob/master/services/location.js"
    click S2 "https://github.com/isaaclopezvidal/qusar-api/blob/master/services/message.js"
    click DM1 "https://github.com/isaaclopezvidal/qusar-api/blob/master/models/AstroNaves.js"
    click DM2 "https://github.com/isaaclopezvidal/qusar-api/blob/master/models/Nave.js"
    click DM3 "https://github.com/isaaclopezvidal/qusar-api/blob/master/models/Posicion.js"
    click DM4 "https://github.com/isaaclopezvidal/qusar-api/blob/master/models/Satelite.js"
    click DM5 "https://github.com/isaaclopezvidal/qusar-api/blob/master/models/Satelites.js"
    click DM6 "https://github.com/isaaclopezvidal/qusar-api/blob/master/models/SatelitesView.js"

    %% Styles
    classDef cloud fill:#FAD7A0,stroke:#333,stroke-width:2px;
    classDef api fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef route fill:#D6EAF8,stroke:#333,stroke-width:2px;
    classDef middleware fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef service fill:#F5B7B1,stroke:#333,stroke-width:2px;
    classDef model fill:#D7BDE2,stroke:#333,stroke-width:2px;
    classDef database fill:#F5CBA7,stroke:#333,stroke-width:2px;
    classDef config fill:#A2DED0,stroke:#333,stroke-width:2px;
    classDef external fill:#E8DAEF,stroke:#333,stroke-width:2px;
```
