How sagoo-cloud/sagooiot works
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