# cywhale/apigate architecture

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

- Interactive diagram: https://gitdiagram.com/cywhale/apigate
- Repository: https://github.com/cywhale/apigate

## Overview

##### Project type and purpose
- This is a Node.js–based backend API service (no frontend) that provides oceanographic data (CTD and SADCP) via both REST and GraphQL endpoints.
- It’s designed to serve clients who request mean data for research vessels around Taiwan. It also includes Swagger for API documentation and can be deployed/managed with PM2.

##### File structure
Top-level:
- server/ – contains the entire application
– package.json & pnpm-lock.yaml – dependency and lock files
– pm2.sh – PM2 deployment script
– src/
- config/ – DB (Knex, Sequelize), cache, Swagger setup
- graphql/ – schema, resolvers, query definitions
- plugins/ – Redis and in-memory cache plugins
- routes/ – REST API route handlers (ctd, sadcp, apirest)
- index.mjs & srvapp.mjs – service initialization & Express/Koa app setup
– tests/ – unit tests for cache and config
Patterns & Indicators:
- Single microservice exposing both REST (routes) and GraphQL endpoints
- Cache layer (Redis, in-memory) to speed up repeated queries
- Database abstraction via Knex and Sequelize
- Swagger for interactive API documentation

##### README for additional insights
- Confirms two main REST API entry points (/api/ctd, /api/sadcp) and live Swagger UI
- Mentions usage examples and DOI/citation

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Layer
    subgraph "External Consumers" 
        direction TB
        Client["Clients (Browser, CURL, Services)"]:::external
        SwaggerUI["Swagger UI (/hub/swagger)"]:::external
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        APIInit["index.mjs"]:::api
        AppSetup["srvapp.mjs"]:::api
        subgraph "REST Controllers"
            direction TB
            APIRest["apirest.mjs"]:::api
            ODBIO["odbio.mjs"]:::api
        end
        subgraph "GraphQL Layer"
            direction TB
            GQLSchema["schema.mjs"]:::api
            GQLResolvers["resolvers.mjs"]:::api
            GQLQueries["bioqry.mjs"]:::api
        end
    end

    %% Business & Cache Layer
    subgraph "Business & Cache Layer"
        direction TB
        StreamBuffer["streamBufferCache.js"]:::business
        StreamCache["streamCache.js"]:::business
        subgraph "Cache Plugins"
            direction TB
            InMemPlugin["cache.js"]:::business
            RedisPlugin["redis.js"]:::business
        end
    end

    %% Data Persistence Layer
    subgraph "Data Layer"
        direction TB
        KnexConn["knexconn.js"]:::data
        Sequelize["sequelizer.js"]:::data
        SQLDB["SQL Database"]:::data
        RedisDB["Redis"]:::data
    end

    %% Management Layer
    subgraph "Management & Deployment"
        direction TB
        PM2["pm2.sh"]:::management
        Package["package.json / pnpm-lock.yaml"]:::management
    end

    %% Connections
    Client -->|HTTP/HTTPS| APIInit
    APIInit --> AppSetup
    AppSetup -->|routes| APIRest
    AppSetup -->|routes| ODBIO
    AppSetup -->|GraphQL Endpoint| GQLSchema
    GQLSchema --> GQLResolvers
    GQLResolvers --> GQLQueries

    APIRest -->|calls| StreamBuffer
    APIRest -->|calls| StreamCache
    ODBIO -->|calls| StreamBuffer
    ODBIO -->|calls| StreamCache
    GQLResolvers -->|uses| StreamBuffer
    GQLResolvers -->|uses| StreamCache

    StreamBuffer -->|GET/SET| InMemPlugin
    StreamBuffer -->|GET/SET| RedisPlugin
    StreamCache -->|GET/SET| InMemPlugin
    StreamCache -->|GET/SET| RedisPlugin

    InMemPlugin -->|cache miss| KnexConn
    InMemPlugin -->|cache miss| Sequelize
    RedisPlugin -->|cache miss| KnexConn
    RedisPlugin -->|cache miss| Sequelize

    KnexConn -->|SQL Query| SQLDB
    Sequelize -->|SQL Query| SQLDB

    AppSetup -->|serves| SwaggerUI

    PM2 -->|manages| AppSetup
    Package -->|defines deps| AppSetup

    %% Click Events
    click APIInit "https://github.com/cywhale/apigate/blob/main/server/src/index.mjs"
    click AppSetup "https://github.com/cywhale/apigate/blob/main/server/src/srvapp.mjs"
    click APIRest "https://github.com/cywhale/apigate/blob/main/server/src/routes/apirest.mjs"
    click ODBIO "https://github.com/cywhale/apigate/blob/main/server/src/routes/odbio.mjs"
    click GQLSchema "https://github.com/cywhale/apigate/blob/main/server/src/graphql/schema.mjs"
    click GQLResolvers "https://github.com/cywhale/apigate/blob/main/server/src/graphql/resolvers.mjs"
    click GQLQueries "https://github.com/cywhale/apigate/blob/main/server/src/graphql/bioqry.mjs"
    click StreamBuffer "https://github.com/cywhale/apigate/blob/main/server/src/config/streamBufferCache.js"
    click StreamCache "https://github.com/cywhale/apigate/blob/main/server/src/config/streamCache.js"
    click InMemPlugin "https://github.com/cywhale/apigate/blob/main/server/src/plugins/cache.js"
    click RedisPlugin "https://github.com/cywhale/apigate/blob/main/server/src/plugins/redis.js"
    click KnexConn "https://github.com/cywhale/apigate/blob/main/server/src/config/knexconn.js"
    click Sequelize "https://github.com/cywhale/apigate/blob/main/server/src/config/sequelizer.js"
    click SwaggerUI "https://github.com/cywhale/apigate/blob/main/server/src/config/swagger_config.js"
    click PM2 "https://github.com/cywhale/apigate/blob/main/server/pm2.sh"
    click Package "https://github.com/cywhale/apigate/blob/main/server/package.json"

    %% Styles
    classDef external fill:#dddddd,stroke:#888888,color:#333333
    classDef api fill:#bbdefb,stroke:#1e88e5,color:#0d47a1
    classDef business fill:#c8e6c9,stroke:#388e3c,color:#1b5e20
    classDef data fill:#ffe0b2,stroke:#fb8c00,color:#e65100
    classDef management fill:#e1bee7,stroke:#8e24aa,color:#4a148c
```
