# gid3onjura/shorai-do-api architecture

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

- Interactive diagram: https://gitdiagram.com/gid3onjura/shorai-do-api
- Repository: https://github.com/gid3onjura/shorai-do-api

## Overview

- API Server: The central entry point is app.js, which initializes the server and ties everything together.
- Database Layer: Represented by the “db” folder. Within this folder, you have:
- Controllers (db/controller): Handling business logic for specific domain areas (e.g., dojo, event, exam, users, refreshtoken).
- Models (db/models): Defining the data structures and schemas for MySQL interaction.
- Database initialization (db/index.js) and SQL scripts (db/data/shorai-do.sql) for setting up the MySQL database.
- Routes: The “routes” folder contains Express route definitions (auth, events, exam, test, users, utils), mapping HTTP endpoints to controllers.
- Middleware: In the “middleware” folder you find tokens and API key handlers that govern authentication and authorization.
- Validation: The “validation” folder provides request schema and validation logic ensuring correct data formats coming into the API.
- Security & Certificates: The “security” folder holds SSL certificate (cert.pem, key.pem) and configuration files to secure connections.
- Documentation: The “doc” directory and README point to API documentation, including a Postman collection and a Doku YAML file.
- Libraries & Helpers: The “lib” directory likely contains utility functions or modules that support other components.
- Mock Data: The “mock” folder provides sample JSON data useful for testing and development.

##### Relationships and Interactions

- Clients (or Postman during testing) send HTTP requests to various endpoints defined in the Routes.
- Each route forwards the request to its corresponding controller (located in db/controller) after passing through Middleware (authentication, API-key resolution, and validation).
- Controllers communicate with the Model layer for database interactions. This is where the business logic is applied.
- The Models interface with the MySQL database (established through db/index.js and configured using credentials provided in an .env file).
- External security layers indicating that HTTPS might be used, employing certificates stored in the “security” folder.
- Optionally, indicate a documentation or utility reference showing how the API is documented (via the “doc” folder and Postman-Collection).

##### Architectural Patterns

- The structure suggests an MVC-like separation:
- Models: Define database entities.
- Controllers: Handle request logic and data processing before interacting with models.
- Routes: Expose endpoints.
- Incorporate Middleware as cross-cutting concerns for authentication and validation.
- The layered approach (Routes → Controllers → Models → Database) should be clearly indicated.

##### Specific Technologies and Tools

- Node.js/Express: The main framework driving the API,
- MySQL: The relational database system (setup via the provided SQL script and config via .env),
- Middleware for Token Authentication and API Key resolution,
- SSL/TLS: For security, using certificates from the “security” folder.
- Documentation Tools: Doku YAML and Postman collection indicate emphasis on well-documented API endpoints.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Clients/Users"]:::client
    APIServer["API Server (app.js)"]:::server
    Client -->|"sends_request"| APIServer

    subgraph "API Server Components"
        direction TB
        Routes["Routes (routes)"]:::route
        Middleware["Middleware (middleware)"]:::middleware
        Controllers["Controllers (db/controller)"]:::controller
        Models["Models (db/models)"]:::model
        Validation["Validation (validation)"]:::validation
        Helpers["Libraries & Helpers (lib)"]:::helper
        Documentation["Documentation (doc & README)"]:::doc
        Security["Security & Certificates (security)"]:::security
        Mock["Mock Data (mock)"]:::mock
    end

    subgraph "Database Layer"
        direction TB
        DBInit["Database Initialization (db/index.js)"]:::db
        SQLScript["SQL Setup (db/data/shorai-do.sql)"]:::db
        MySQL["MySQL Database"]:::db
    end

    APIServer -->|"initializes"| Routes
    Routes -->|"passes_through"| Middleware
    Middleware -->|"calls"| Controllers
    Controllers -->|"interacts_with"| Models
    Models -->|"queries"| DBInit
    DBInit -->|"executes"| SQLScript
    SQLScript -->|"sets_up"| MySQL

    Routes -->|"validates"| Validation
    Routes -->|"utilizes"| Helpers

    APIServer -->|"secures"| Security
    APIServer -->|"serves_docs"| Documentation
    APIServer -->|"uses_for_testing"| Mock

    click APIServer "https://github.com/gid3onjura/shorai-do-api/blob/master/app.js"
    click Controllers "https://github.com/gid3onjura/shorai-do-api/tree/master/db/controller"
    click Models "https://github.com/gid3onjura/shorai-do-api/tree/master/db/models"
    click DBInit "https://github.com/gid3onjura/shorai-do-api/blob/master/db/index.js"
    click SQLScript "https://github.com/gid3onjura/shorai-do-api/blob/master/db/data/shorai-do.sql"
    click Routes "https://github.com/gid3onjura/shorai-do-api/tree/master/routes"
    click Middleware "https://github.com/gid3onjura/shorai-do-api/tree/master/middleware"
    click Validation "https://github.com/gid3onjura/shorai-do-api/tree/master/validation"
    click Security "https://github.com/gid3onjura/shorai-do-api/tree/master/security"
    click Documentation "https://github.com/gid3onjura/shorai-do-api/tree/master/doc"
    click Helpers "https://github.com/gid3onjura/shorai-do-api/tree/master/lib"
    click Mock "https://github.com/gid3onjura/shorai-do-api/tree/master/mock"

    classDef client fill:#f9c,stroke:#333,stroke-width:2px;
    classDef server fill:#ccf,stroke:#333,stroke-width:2px;
    classDef route fill:#cfc,stroke:#333,stroke-width:2px;
    classDef middleware fill:#fcf,stroke:#333,stroke-width:2px;
    classDef controller fill:#cff,stroke:#333,stroke-width:2px;
    classDef model fill:#ffc,stroke:#333,stroke-width:2px;
    classDef db fill:#fdd,stroke:#933,stroke-width:2px;
    classDef validation fill:#eee,stroke:#333,stroke-width:2px;
    classDef helper fill:#efe,stroke:#333,stroke-width:2px;
    classDef doc fill:#ddf,stroke:#333,stroke-width:2px;
    classDef security fill:#fed,stroke:#333,stroke-width:2px;
    classDef mock fill:#def,stroke:#333,stroke-width:2px;
```
