# sagoo-cloud/sagooiot architecture

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

- Interactive diagram: https://gitdiagram.com/sagoo-cloud/sagooiot
- Repository: https://github.com/sagoo-cloud/sagooiot

## Overview

##### Project Type and Purpose
- This is the backend of a full-stack IoT management platform (SagooIOT Community V2), written in Go using GoFrame 2.0, exposing RESTful APIs to a Vue3/ElementPlus frontend.
- It handles device access (TCP, MQTT, CoAP, HTTP, WebSocket), data processing, rule engines, notifications, user management, and persists data in MySQL, TDEngine (time-series), and Redis.

##### File Structure
Top-level containers:
- api/v1 ‑ API definitions by domain (alarm, analysis, common, network, notice, product, system, tdengine, websocket)
- internal/cmd & router ‑ application entrypoint and routing setup
- internal/controller ‑ HTTP handlers invoking business logic
- internal/logic ‑ core business logic, including middleware, context management, analysis, alarm rule engine, network tunnel logic, etc.
- internal/dao & model ‑ data access layer and ORM-style models for MySQL and other stores
- internal/service ‑ service composition (aggregates logic and DAO to fulfill use cases)
- network/… ‑ standalone network-core library for device-side protocols, tunnels, and transport
- pkg/… ‑ reusable packages (cache, dcache, mqttclient, plugin framework, tsd factory for time-series DB, utility functions)
- internal/queues, internal/workers, internal/tasks ‑ asynchronous job queues and scheduled/background workers
- manifest/docker-compose & kustomize ‑ deployment descriptors for containers (MySQL, Redis, Nginx, the “iot-open” API service)

##### README Insights
- Backend framework: GoFrame 2.0
- Auth: gtoken + Casbin
- Plugin system: hot-pluggable C/C++/Python modules for protocols and data acquisition
- Supports multiple network transports (TCP, MQTT, UDP, CoAP, HTTP, gRPC)
- Components: user/role/department management, product/device management, rule engine, notifications, code generator, monitoring, file upload, cache, scheduled tasks, SSE/WebSocket pushing

a. Main Components (Containers)
- Browser / Front-end UI (Vue3 + ElementPlus)
- Nginx (static assets & reverse proxy)
- IoT API Service (“iot-open” GoFrame binary)
- Network-Core Service (embedded or library inside API service for device transports)
- Plugin Manager & Plugin Modules (protocol extensions, hot-plugged)
- Scheduler & Worker Pool (background tasks, queue consumers)
- Caches & Queues (Redis, in-memory caches)
- Databases: MySQL (relational), TDEngine (time-series)

b. Internal Layers (inside the IoT API Service)
- API Layer (api/v1 definitions → controllers)
- Controller Layer (internal/controller → parse requests, auth)
- Service Layer (internal/service → orchestrate logic + DAO)
- Logic Layer (internal/logic → business rules, alarm engine, analysis)
- DAO/Model Layer (internal/dao + internal/model → persistence to MySQL/TDEngine)
- Utility Packages (pkg/* → caching, JWT, pooling, tsd factory)
- Network-Core Library (network/* → handles device-protocol connectivity)

c. Relationships & Data Flows
- Browser → HTTP(S) → Nginx → HTTP → API Controller
- Controller → Service → Logic → DAO → MySQL / TDEngine
- Logic exactly: alarm rule checks, analysis, notifications → push via WebSocket/SSE → Browser
- Device → MQTT/TCP/UDP (network-core) → internal logic → DAO → DB
- Scheduled/Queue tasks (e.g., data archiving) → Worker Pool → DAO → DB
- Caching layer (Redis/pkg/dcache) accelerates reads/writes for hot data and session storage
- Plugin modules loaded at runtime by Plugin Manager → extend protocol support

d. Architectural Patterns & Design Principles
- Layered architecture: clear separation of API, controller, service, logic, DAO/model
- Modularization by domain (alarm, analysis, common, network, notice, product, system)
- Hexagonal/in-port–out-port style: pkg and internal packages define clear interfaces
- Event-driven for notifications (SSE/WebSocket), rule-engine cron tasks
- Asynchronous processing: internal/queues and workers

e. Key Technologies & Frameworks
- GoFrame 2.0 (web framework, router auto-docs)
- Vue3 + Vite + TypeScript + ElementPlus (front-end)
- MySQL (relational), TDEngine (time-series), Redis (cache, queue)
- MQTT client & server, WebSocket, SSE for real-time streaming
- gtoken + Casbin for auth and RBAC
- Docker Compose & Kustomize for container orchestration

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External UI and Gateway
    Browser["Browser UI (Vue3 + ElementPlus)"]:::external
    Nginx["Nginx Gateway"]:::gateway
    Browser -->|"HTTP(S)"| Nginx
    Nginx -->|"HTTP" | Entry

    %% Backend Service Container
    subgraph "IoT API Service" 
        direction TB
        Entry["Entrypoint\n(main.go)"]:::service
        API["API Layer\n(api/v1)"]:::api
        Controller["Controller Layer\n(internal/controller)"]:::controller
        Service["Service Layer\n(internal/service)"]:::service
        Logic["Logic Layer\n(internal/logic)"]:::logic
        DAO["DAO Layer\n(internal/dao)"]:::dao
        Model["Model Layer\n(internal/model)"]:::model
        NetworkCore["Network-Core Library\n(network/core)"]:::network
        Plugins["Plugin Manager & Modules\n(pkg/plugins)"]:::plugin
        Auth["Auth & Authz\n(pkg/gftoken\ncommon/check_auth.go)"]:::auth
        Cache["Caching (Redis)\n(pkg/cache)"]:::cache
        Dcache["Distributed Cache\n(pkg/dcache)"]:::cache
        TSD["Time-Series DB Factory\n(pkg/tsd)"]:::database
        MQTT["MQTT Client Support\n(pkg/mqttclient)"]:::network
        SSE["SSE/Event Streaming\n(internal/sse)"]:::stream
        WS["WebSocket Pushing\n(internal/webscoket)"]:::stream
        Queues["Async Queues\n(internal/queues)"]:::background
        Tasks["Tasks\n(internal/tasks)"]:::background
        Workers["Worker Pool\n(internal/workers)"]:::background

        Entry --> API
        API --> Controller
        Controller --> Service
        Service --> Logic
        Logic --> DAO
        DAO --> Model

        %% Utilities & Extensions
        Logic --> Auth
        Service --> Cache
        Service --> Dcache
        Service --> TSD
        Service --> MQTT
        Logic --> Plugins
        Logic --> NetworkCore
        Logic --> SSE
        Logic --> WS

        %% Background flows
        Queues --> Tasks
        Tasks --> Workers
        Workers --> DAO
    end

    %% External Data Sources
    subgraph "Data Stores"
        direction TB
        MySQL["MySQL"]:::database
        TDEngine["TDEngine"]:::database
        Redis["Redis"]:::database
    end

    %% Device Connectivity
    subgraph "IoT Devices & Protocols"
        direction TB
        Devices["IoT Devices\n(TCP/MQTT/CoAP/HTTP/WebSocket)"]:::device
    end

    %% Connections between service and data stores & devices
    DAO -->|"ORM Read/Write"| MySQL
    Service -->|"Time-Series Queries"| TDEngine
    Cache -->|"Cache Reads/Writes"| Redis
    Devices -->|"MQTT/TCP/etc."| NetworkCore
    NetworkCore --> Logic

    %% Click Events
    click Entry "https://github.com/sagoo-cloud/sagooiot/blob/main/main.go"
    click Entry "https://github.com/sagoo-cloud/sagooiot/blob/main/internal/cmd/server.go"
    click Entry "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/cmd/router"
    click API "https://github.com/sagoo-cloud/sagooiot/tree/main/api/v1"
    click Controller "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/controller"
    click Service "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/service"
    click Logic "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/logic"
    click DAO "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/dao"
    click Model "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/model"
    click NetworkCore "https://github.com/sagoo-cloud/sagooiot/tree/main/network/core"
    click Plugins "https://github.com/sagoo-cloud/sagooiot/tree/main/pkg/plugins"
    click Auth "https://github.com/sagoo-cloud/sagooiot/tree/main/pkg/gftoken"
    click Auth "https://github.com/sagoo-cloud/sagooiot/blob/main/internal/logic/common/check_auth.go"
    click Cache "https://github.com/sagoo-cloud/sagooiot/tree/main/pkg/cache"
    click Dcache "https://github.com/sagoo-cloud/sagooiot/tree/main/pkg/dcache"
    click TSD "https://github.com/sagoo-cloud/sagooiot/tree/main/pkg/tsd"
    click MQTT "https://github.com/sagoo-cloud/sagooiot/tree/main/pkg/mqttclient"
    click SSE "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/sse"
    click WS "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/webscoket"
    click Queues "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/queues"
    click Tasks "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/tasks"
    click Workers "https://github.com/sagoo-cloud/sagooiot/tree/main/internal/workers"
    click Docker "https://github.com/sagoo-cloud/sagooiot/tree/main/manifest/docker-compose"
    click Docker "https://github.com/sagoo-cloud/sagooiot/tree/main/manifest/deploy/kustomize"

    %% Styles
    classDef external fill:#E0F7FA,stroke:#006064;
    classDef gateway fill:#B2EBF2,stroke:#0097A7;
    classDef service fill:#BBDEFB,stroke:#1E88E5;
    classDef api fill:#90CAF9,stroke:#1565C0;
    classDef controller fill:#64B5F6,stroke:#0D47A1;
    classDef logic fill:#FFE0B2,stroke:#F57C00;
    classDef service fill:#BBDEFB,stroke:#1976D2;
    classDef dao fill:#C8E6C9,stroke:#2E7D32;
    classDef model fill:#DCEDC8,stroke:#33691E;
    classDef network fill:#FFF9C4,stroke:#F9A825;
    classDef plugin fill:#E1BEE7,stroke:#8E24AA;
    classDef auth fill:#F8BBD0,stroke:#C2185B;
    classDef cache fill:#D1C4E9,stroke:#5E35B1;
    classDef database fill:#A5D6A7,stroke:#388E3C;
    classDef stream fill:#FFCC80,stroke:#EF6C00;
    classDef background fill:#CFD8DC,stroke:#546E7A;
    classDef device fill:#F0F4C3,stroke:#827717;
```
