# adamlounds/nightscout-go architecture

> Architecture diagram of the GitHub repository adamlounds/nightscout-go, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/adamlounds/nightscout-go
- Repository: https://github.com/adamlounds/nightscout-go

## Overview

##### Main Components

a) External Clients Layer:
- Nightscout MacOS Menu Bar
- Nightscout-LibreLink-Up
- Scoutnight
- Nightguard iOS app
- Other API consumers

b) API Layer:
- API v1 endpoints (controllers/api1.go)
- Authentication middleware (controllers/api1_auth_middleware.go)

c) Core Service Layer:
- Entry management
- Treatment management
- Authentication handling

d) Repository/Storage Layer:
- Bucket storage (S3/R2) adapter
- CGM LibreLink-Up integration
- Nightscout compatibility layer

**┌─────────────────────────────────────────────────────────────┐**
- External Clients                        │
- ┘
- ↓ HTTPS
**┌─────────────────────────────────────────────────────────────┐**
- API Layer (v1)                         │
- ┌─────────────┐  ┌──────────────┐  ┌──────────────────┐   │
- Entry APIs  │  │Treatment APIs│  │Authentication APIs│   │
- ┘  └──────────────┘  └──────────────────┘   │
- ┘
- ↓
**┌─────────────────────────────────────────────────────────────┐**
- Service Layer                            │
- ┌─────────────┐  ┌──────────────┐  ┌──────────────────┐   │
- Entry Service│  │Treatment Svc │  │   Auth Service   │   │
- ┘  └──────────────┘  └──────────────────┘   │
- ┘
- ↓
**┌─────────────────────────────────────────────────────────────┐**
- Repository Layer                           │
- ┌─────────────┐  ┌──────────────┐  ┌──────────────────┐   │
- Bucket Store │  │CGM LibreLink │  │   Nightscout    │   │
- (S3/R2)    │  │     Up       │  │   Repository    │   │
- ┘  └──────────────┘  └──────────────────┘   │
- ┘

##### Important Architectural Patterns to Highlight

a) Repository Pattern:
- Clear separation between storage implementations
- Pluggable nature of storage adapters

b) Middleware Chain:
- Authentication flow
- How API requests are processed

##### Data Flow Indicators

- How data flows from CGM LibreLink-Up to storage
- How clients retrieve data through the API
- How authentication is verified for each request

##### Technical Details to Note

- Relationship between the cmd/server component and other layers
- Where configuration (config/config.go) affects different components
- Where tests exist (particularly for entry_bucket_repository_test.go)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef external fill:#3498db,stroke:#2980b9,color:white
    classDef api fill:#2ecc71,stroke:#27ae60,color:white
    classDef service fill:#f1c40f,stroke:#f39c12,color:black
    classDef repository fill:#e67e22,stroke:#d35400,color:white
    classDef auth fill:#e74c3c,stroke:#c0392b,color:white
    
    %% External Clients Layer
    subgraph Clients
        NS_MAC["Nightscout MacOS Menu Bar"]
        NS_LIB["Nightscout-LibreLink-Up"]
        SCOUT["Scoutnight"]
        NG_IOS["Nightguard iOS app"]
        OTHER["Other API Consumers"]
    end
    class NS_MAC,NS_LIB,SCOUT,NG_IOS,OTHER external

    %% API Layer
    subgraph API
        API_ENTRY["Entry APIs"]
        API_TREAT["Treatment APIs"]
        API_AUTH["Authentication APIs"]
        AUTH_MID["Auth Middleware"]
    end
    class API_ENTRY,API_TREAT,API_AUTH,AUTH_MID api
    click API_ENTRY "https://github.com/adamlounds/nightscout-go/blob/main/controllers/api1.go"
    click AUTH_MID "https://github.com/adamlounds/nightscout-go/blob/main/controllers/api1_auth_middleware.go"
    click AUTH_MID "https://github.com/adamlounds/nightscout-go/blob/main/middleware/authentication.go"

    %% Service Layer
    subgraph Services
        SVC_ENTRY["Entry Service"]
        SVC_TREAT["Treatment Service"]
        SVC_AUTH["Auth Service"]
    end
    class SVC_ENTRY,SVC_TREAT,SVC_AUTH service
    click SVC_ENTRY "https://github.com/adamlounds/nightscout-go/blob/main/models/entry.go"
    click SVC_TREAT "https://github.com/adamlounds/nightscout-go/blob/main/models/treatment.go"
    click SVC_AUTH "https://github.com/adamlounds/nightscout-go/blob/main/models/auth.go"

    %% Repository Layer
    subgraph Repositories
        BUCKET["Bucket Store"]
        CGM_LIB["CGM LibreLink Up"]
        NS_REPO["Nightscout Repository"]
        TREAT_REPO["Treatment Repository"]
        AUTH_REPO["Auth Repository"]
    end
    class BUCKET,CGM_LIB,NS_REPO,TREAT_REPO,AUTH_REPO repository
    click BUCKET "https://github.com/adamlounds/nightscout-go/blob/main/stores/bucket/bucket.go"
    click CGM_LIB "https://github.com/adamlounds/nightscout-go/blob/main/stores/cgmlibrelinkup/cgmlibrelinkup.go"
    click NS_REPO "https://github.com/adamlounds/nightscout-go/blob/main/stores/nightscout/nightscout.go"
    click TREAT_REPO "https://github.com/adamlounds/nightscout-go/blob/main/adapters/treatment_bucket_repository.go"
    click AUTH_REPO "https://github.com/adamlounds/nightscout-go/blob/main/adapters/auth_bucket_repository.go"

    %% Connections
    Clients --> AUTH_MID
    AUTH_MID --> API_ENTRY & API_TREAT & API_AUTH
    
    API_ENTRY --> SVC_ENTRY
    API_TREAT --> SVC_TREAT
    API_AUTH --> SVC_AUTH
    
    SVC_ENTRY --> BUCKET & CGM_LIB & NS_REPO
    SVC_TREAT --> TREAT_REPO
    SVC_AUTH --> AUTH_REPO

    %% Config Connection
    CONFIG["Configuration"]
    CONFIG --> API & Services & Repositories
    class CONFIG service
    click CONFIG "https://github.com/adamlounds/nightscout-go/blob/main/config/config.go"

    %% Server Connection
    SERVER["Server"]
    SERVER --> API
    class SERVER service
    click SERVER "https://github.com/adamlounds/nightscout-go/blob/main/cmd/server/server.go"

    %% Legend
    subgraph Legend
        EXT["External Systems"]:::external
        API_L["API Components"]:::api
        SVC_L["Service Components"]:::service
        REPO_L["Repository Components"]:::repository
        AUTH_L["Auth Components"]:::auth
    end
```
