# bobac/nsight-proxy architecture

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

- Interactive diagram: https://gitdiagram.com/bobac/nsight-proxy
- Repository: https://github.com/bobac/nsight-proxy

## Overview

##### Main Components
a. Configuration Loader
– Loads `.env` variables (`NSIGHT_API_KEY`, `NSIGHT_SERVER`)
b. CLI Front-Ends (in cmd/):
- getdata – single‐service JSON fetcher
- fetchall – bulk data fetcher + CSV cache + nested JSON output
c. API Server (in cmd/server) – HTTP server scaffold for future REST endpoints
d. Internal Library (internal/nsight):
- api.go – Go client for N-Able N-Sight Data Extraction API
- types.go – request/response structs
e. Cache Storage (data/ CSV files)
- clients.csv, sites.csv, servers.csv, workstations.csv
f. External N-Able N-Sight API – upstream data source

##### Out Data Flows & Interactions
a. getdata Flow (no cache):
[getdata CLI]
→ loads config (.env)
→ calls [internal/nsight client]
→ issues HTTP request → [External N-Able API]
← receives JSON → returns JSON to stdout
b. fetchall Flow:
– Without –cache flag:
[fetchall CLI]
→ loads config
→ for each resource (clients, sites, servers, workstations):
→ internal/nsight client → External API → raw JSON
→ transform & append rows → [CSV Cache Storage]
→ read aggregated CSVs → build nested JSON → stdout or file
– With –cache flag:
[fetchall CLI]
→ loads only CSV files from [CSV Cache Storage]
→ build nested JSON → stdout or file
c. API Server (future):
[HTTP Client]
→ HTTP request → [API Server CLI]
→ loads config
→ either:
- call internal/nsight → External API
- read CSV cache → return JSON
→ HTTP response JSON

##### Architectural Patterns
– Layered Architecture:
- Presentation (CLI & HTTP server)
- Application/Domain (internal/nsight library)
- Infrastructure (HTTP transport to external API, file system CSV cache)
– Separation of Concerns: each CLI has one responsibility, sharing the same client library.
– Cache-Aside Pattern in `fetchall` (populate CSV cache then read from it).

##### Relevant Technologies
– Go Modules (`go.mod`, `go.sum`)
– Go toolchain (`go run`, `build.sh`)
– Environment config (`.env`, loaded via Go)
– CSV files on disk (ignored by Git via `.gitignore`)
– Future HTTP server on port 8080 (net/http)

##### Final Checklist
[ ] Annotation of protocols (HTTP, file I/O)
[ ] Layer separation: Presentation → Domain → Infrastructure

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "cmd/"
        direction TB
        getdata["getdata CLI"]:::exec
        fetchall["fetchall CLI"]:::exec
        server["API Server (future)"]:::exec
    end

    %% Shared Library Layer
    subgraph "Shared Library"
        direction TB
        Config["Configuration Loader\n(.env)"]:::shared
        API["API Client"]:::shared
        Types["Request/Response Types"]:::shared
    end

    %% Infrastructure Layer
    CSV["CSV Cache Storage\n(data/*.csv)"]:::cache
    External["External N-Able API"]:::external
    HTTPClient["HTTP Client"]:::exec

    %% Flows for getdata
    getdata -->|"loads config"| Config
    getdata -->|"calls client"| API
    API -->|"HTTP/JSON"| External
    External -->|"JSON response"| API
    API -->|"returns JSON"| getdata

    %% Flows for fetchall (without cache flag)
    fetchall -->|"loads config"| Config
    fetchall -->|"calls client per resource"| API
    API -->|"HTTP/JSON"| External
    External -->|"JSON response"| API
    API -->|"transform & append rows"| CSV
    CSV ---|"read aggregated CSVs"| fetchall
    fetchall -->|"build nested JSON"| fetchall

    %% Flows for fetchall (with cache flag)
    fetchall -->|"load CSV only"| CSV
    CSV ---|"read CSV files"| fetchall
    fetchall -->|"build nested JSON"| fetchall

    %% Flows for API Server
    HTTPClient -->|"HTTP request"| server
    server -->|"loads config"| Config
    server -->|"on request: call client"| API
    API -->|"HTTP/JSON"| External
    External -->|"JSON response"| API
    API -->|"return JSON"| server
    server -->|"HTTP response JSON"| HTTPClient
    server -->|"or read cache"| CSV
    CSV ---|"read CSV files"| server

    %% Click Events
    click Config "https://github.com/bobac/nsight-proxy/blob/main/.env.example"
    click getdata "https://github.com/bobac/nsight-proxy/blob/main/cmd/getdata/main.go"
    click fetchall "https://github.com/bobac/nsight-proxy/blob/main/cmd/fetchall/main.go"
    click server "https://github.com/bobac/nsight-proxy/blob/main/cmd/server/main.go"
    click API "https://github.com/bobac/nsight-proxy/blob/main/internal/nsight/api.go"
    click Types "https://github.com/bobac/nsight-proxy/blob/main/internal/nsight/types.go"

    %% Styles
    classDef exec fill:#ADD8E6,stroke:#000
    classDef shared fill:#90EE90,stroke:#000
    classDef cache fill:#FFA500,stroke:#000
    classDef external fill:#D3D3D3,stroke:#000 continuous
```
