# maojianwei/mao-service-discovery architecture

> Architecture diagram of the GitHub repository maojianwei/mao-service-discovery, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/maojianwei/mao-service-discovery
- Repository: https://github.com/maojianwei/mao-service-discovery

## Overview

1. **Repository shape**
   This repo is a Go-based service-discovery/monitoring system with a **single core backend binary** and a **separate Vue 3 WebUI**. It also produces packaged runtime artifacts for Linux, Windows, Raspberry Pi, and Docker.

2. **Main runtime entrypoints**
   The executable is assembled from `main.go`, `cmd/server.go`, and the client entrypoints in `cmd/general_client.go` / `cmd/general_client_v2.go`. The CLI is mode-driven: **client mode** reports discovery data, while **server mode** aggregates, exposes APIs, and serves the UI.

3. **Discovery and heartbeat plane**
   The primary signal sources are implemented as dedicated modules under `cmd/lib/IcmpKa` and `cmd/lib/GrpcKa`, with server-side wrappers in `cmd/api/icmp-ka-module.go` and `cmd/api/grpc-ka-module.go`. These are the core monitoring mechanisms:
   - **ICMP-based detection** for server-only monitoring
   - **gRPC stream-based reporting** for client-server mode
   The README notes ICMP requires `CAP_NET_RAW` or root privileges.

4. **API and orchestration layer**
   The `cmd/api` directory is the central orchestration boundary. It wires together:
   - configuration
   - ICMP and gRPC keepalive/detection
   - REST exposure
   - auxiliary data collection
   - topology/gateway integrations
   - notification integrations
   This layer is where raw discovery data becomes user-facing service state.

5. **REST and UI serving boundary**
   `cmd/lib/Restful` and `cmd/api/restful-server-module.go` define the HTTP/JSON interface used by the WebUI and other consumers. The server likely serves both:
   - structured API responses
   - static web assets from `resource/static` and HTML templates from `resource/html`

6. **Domain data model**
   The `cmd/api/data` package holds typed domain data such as `gps`, `nat66`, and `temperature`. This suggests the system tracks more than reachability; it also carries environment and metadata fields that feed dashboards and alerts.

7. **Persistence and external telemetry**
   `cmd/lib/InfluxDB` is the persistence/metrics utility layer, and the README shows server mode can be configured with InfluxDB connection parameters. This indicates time-series storage is an important backend boundary for historical monitoring data.

8. **Notifications and integrations**
   Several modules connect the discovery system to external services:
   - `cmd/lib/Email` and `cmd/api/email-module.go` for SMTP alerts
   - `incubator/Wechat` and `cmd/api/wechat-module.go` for WeChat notifications
   - `incubator/MaoCloudMonitor` for cloud monitor integration
   These are side-effecting outputs from the monitoring pipeline.

9. **Network/device and topology integrations**
   `cmd/lib/Soap` and `cmd/api/gateway-module.go` point to TP-Link gateway control via SOAP, while `cmd/api/topo-module.go` and `incubator/OnosTopoShow` suggest topology visualization/integration with ONOS. These are adjunct subsystems attached to the core discovery data, not the discovery engine itself.

10. **Service registry and shared utilities**
   `cmd/lib/MaoCommon` contains service-registry utilities, and `grpc.maojianwei.com/server/discovery/api` contains the protobuf contract and generated gRPC bindings. `util/` provides logging and generic helpers. These packages form the shared foundation used across client, server, and integrations.

11. **WebUI architecture**
   The `webui/` directory is a standalone Vue 3 app with route/page components for ICMP, email, MySQL, topology, gRPC, and server status views. Its build output is checked into `resource/static`, which means the Go backend can serve the UI without a separate frontend server at runtime.

12. **Experimental / non-core code**
   `incubator/` and `bak/` contain experimental, archived, or backup implementations, including etcd usage, MySQL sync publishing, and older ICMP/test code. These are useful for understanding evolution, but they are not the main production path.

13. **Deployment and packaging boundary**
   The repo is built for multiple distribution formats via `build_all.sh`, `statically_linked_compilation.sh`, `Dockerfile`, `.goreleaser.yaml`, and the `.npm-release-binary*` package directories. That means the runtime is a Go binary, but delivery happens through Docker and NPM-wrapped platform-specific binaries.

14. **Overall data flow**
   The most important architecture path is:
   **CLI mode / client probes / server probes → protocol modules (ICMP, gRPC) → orchestration in `cmd/api` → storage/notification/integration services → REST/JSON API + static WebUI**
   This is the flow an architecture diagram should emphasize.

Main entry points: [`cmd/api`](https://github.com/maojianwei/mao-service-discovery/tree/master/cmd/api) (API core), [`cmd/api/restful-server-module.go`](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/restful-server-module.go) (REST API), [`main.go`](https://github.com/maojianwei/mao-service-discovery/blob/master/main.go) (Main), [`Dockerfile`](https://github.com/maojianwei/mao-service-discovery/tree/master/Dockerfile) (Release pack)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main["Main<br/>Go entrypoint<br/>[main.go]"]
  node_server_cmd["Server cmd<br/>Go server<br/>[server.go]"]
  node_client_cmd["Client mode<br/>Go client<br/>[general_client.go]"]
  node_client_v2_cmd["Client v2<br/>Go client"]
end

subgraph group_core["Core API"]
  node_api_core["API core<br/>Orchestrator"]
  node_config_mod["Config<br/>[config-module.go]"]
  node_restful_mod["REST API<br/>HTTP boundary"]
  node_aux_mod["Aux data<br/>Processor<br/>[aux-data-module.go]"]
end

subgraph group_signals["Signals"]
  node_icmp_mod["ICMP module<br/>Probe adapter<br/>[icmp-ka-module.go]"]
  node_grpc_mod["gRPC module<br/>Stream adapter<br/>[grpc-ka-module.go]"]
  node_icmp_lib["ICMP detect<br/>Probe engine"]
  node_grpc_lib["gRPC detect<br/>Probe engine"]
end

subgraph group_integrations["Integrations"]
  node_influx[("InfluxDB<br/>Time-series util<br/>[influxdb-util.go]")]
  node_email_mod["Email<br/>Alerting module<br/>[email-module.go]"]
  node_wechat_mod["WeChat<br/>Alerting module<br/>[wechat-module.go]"]
  node_gateway_mod["Gateway<br/>Device integration<br/>[gateway-module.go]"]
  node_topo_mod["Topology<br/>[topo-module.go]"]
  node_cloud_mod["Cloud monitor<br/>Wrapper"]
end

subgraph group_shared["Shared Libs"]
  node_service_registry["Registry<br/>Shared utility"]
  node_restful_lib["Restful lib<br/>HTTP utility"]
  node_proto_api["Proto API<br/>gRPC contract"]
  node_util_pkg["Util<br/>Helpers<br/>[mao_util.go]"]
end

subgraph group_ui["WebUI"]
  node_webui_app["WebUI<br/>Vue 3 app<br/>[App.vue]"]
  node_webui_router["Router<br/>Frontend routing<br/>[mao-router.js]"]
  node_static_assets["Static assets<br/>Built UI"]
  node_html_templates["HTML templates<br/>[index.html]"]
end

subgraph group_packaging["Packaging"]
  node_release_pack["Release pack<br/>Packaging"]
end

subgraph group_experimental["Experimental"]
  node_incubator["Incubator<br/>Experimental"]
  node_bak["Backup code<br/>Archived"]
end

node_main -->|"server mode"| node_server_cmd
node_main -->|"client mode"| node_client_cmd
node_main -->|"client v2"| node_client_v2_cmd
node_server_cmd -->|"orchestrates"| node_api_core
node_client_cmd -->|"reports via"| node_grpc_lib
node_client_v2_cmd -->|"reports via"| node_grpc_lib
node_api_core -->|"loads"| node_config_mod
node_api_core -->|"wires"| node_icmp_mod
node_api_core -->|"wires"| node_grpc_mod
node_api_core -->|"exposes"| node_restful_mod
node_api_core -->|"enriches"| node_aux_mod
node_api_core -->|"alerts"| node_email_mod
node_api_core -->|"alerts"| node_wechat_mod
node_api_core -->|"integrates"| node_gateway_mod
node_api_core -->|"integrates"| node_topo_mod
node_api_core -->|"integrates"| node_cloud_mod
node_icmp_mod -->|"uses"| node_icmp_lib
node_grpc_mod -->|"uses"| node_grpc_lib
node_restful_mod -->|"implements"| node_restful_lib
node_restful_mod -->|"serves"| node_webui_app
node_restful_mod -->|"serves"| node_static_assets
node_restful_mod -->|"renders"| node_html_templates
node_api_core -->|"persists"| node_influx
node_api_core -->|"shares"| node_service_registry
node_api_core -->|"contracts"| node_proto_api
node_api_core -->|"logs/util"| node_util_pkg
node_webui_app -->|"navigates"| node_webui_router
node_release_pack -->|"bundles"| node_static_assets
node_release_pack -->|"builds"| node_webui_app
node_release_pack -->|"ships"| node_main
node_incubator -.->|"adjacent"| node_api_core
node_bak -.->|"history"| node_icmp_mod

click node_main "https://github.com/maojianwei/mao-service-discovery/blob/master/main.go"
click node_server_cmd "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/server.go"
click node_client_cmd "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/general_client.go"
click node_client_v2_cmd "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/general_client_v2.go"
click node_api_core "https://github.com/maojianwei/mao-service-discovery/tree/master/cmd/api"
click node_config_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/config-module.go"
click node_restful_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/restful-server-module.go"
click node_aux_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/aux-data-module.go"
click node_icmp_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/icmp-ka-module.go"
click node_grpc_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/grpc-ka-module.go"
click node_icmp_lib "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/IcmpKa/icmp-detect-module.go"
click node_grpc_lib "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/GrpcKa/grpc-detect-module.go"
click node_influx "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/InfluxDB/influxdb-util.go"
click node_email_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/email-module.go"
click node_wechat_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/wechat-module.go"
click node_gateway_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/gateway-module.go"
click node_topo_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/topo-module.go"
click node_cloud_mod "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/mao-cloud-monitor-wrapper-module.go"
click node_service_registry "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/MaoCommon/service-registry.go"
click node_restful_lib "https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/Restful/restful-server-impl.go"
click node_proto_api "https://github.com/maojianwei/mao-service-discovery/blob/master/grpc.maojianwei.com/server/discovery/api/mao-server-discovery.proto"
click node_util_pkg "https://github.com/maojianwei/mao-service-discovery/blob/master/util/mao_util.go"
click node_webui_app "https://github.com/maojianwei/mao-service-discovery/blob/master/webui/src/App.vue"
click node_webui_router "https://github.com/maojianwei/mao-service-discovery/blob/master/webui/src/mao-router.js"
click node_static_assets "https://github.com/maojianwei/mao-service-discovery/tree/master/resource/static"
click node_html_templates "https://github.com/maojianwei/mao-service-discovery/blob/master/resource/html/index.html"
click node_release_pack "https://github.com/maojianwei/mao-service-discovery/tree/master/Dockerfile"
click node_incubator "https://github.com/maojianwei/mao-service-discovery/tree/master/incubator"
click node_bak "https://github.com/maojianwei/mao-service-discovery/tree/master/bak"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_server_cmd,node_client_cmd,node_client_v2_cmd,node_release_pack toneBlue
class node_api_core,node_config_mod,node_restful_mod,node_aux_mod,node_incubator,node_bak toneAmber
class node_icmp_mod,node_grpc_mod,node_icmp_lib,node_grpc_lib toneMint
class node_influx,node_email_mod,node_wechat_mod,node_gateway_mod,node_topo_mod,node_cloud_mod toneRose
class node_service_registry,node_restful_lib,node_proto_api,node_util_pkg toneIndigo
class node_webui_app,node_webui_router,node_static_assets,node_html_templates toneTeal
```

## Components

### Runtime

Go entrypoints and mode selection

- [Main](https://github.com/maojianwei/mao-service-discovery/blob/master/main.go): Go entrypoint · Process bootstrap and handoff into the mode-driven runtime · `main.go`
- [Server cmd](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/server.go): Go server · Server-mode entrypoint that aggregates monitoring data and serves APIs · `cmd/server.go`
- [Client mode](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/general_client.go): Go client · Client-mode entrypoint for reporting discovery data · `cmd/general_client.go`
- [Client v2](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/general_client_v2.go): Go client · Alternative client entrypoint for the discovery/reporting path · `cmd/general_client_v2.go`

### Core API

Server orchestration and domain-facing modules

- [API core](https://github.com/maojianwei/mao-service-discovery/tree/master/cmd/api): Orchestrator · Central wiring layer that combines config, discovery, storage, notifications, and UI-facing services · `cmd/api`
- [Config](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/config-module.go): Module · Configuration loading and normalization for runtime behavior · `cmd/api/config-module.go`
- [REST API](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/restful-server-module.go): HTTP boundary · JSON/HTTP service boundary used by the WebUI and other consumers · `cmd/api/restful-server-module.go`
- [Aux data](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/aux-data-module.go): Processor · Secondary data collection and enrichment pipeline · `cmd/api/aux-data-module.go`

### Signals

Discovery and keepalive mechanisms

- [ICMP module](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/icmp-ka-module.go): Probe adapter · Server-side wrapper around ICMP keepalive/detection · `cmd/api/icmp-ka-module.go`
- [gRPC module](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/grpc-ka-module.go): Stream adapter · Server-side wrapper around gRPC-based client reporting · `cmd/api/grpc-ka-module.go`
- [ICMP detect](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/IcmpKa/icmp-detect-module.go): Probe engine · Low-level ICMP reachability and heartbeat detection · `cmd/lib/IcmpKa/icmp-detect-module.go`
- [gRPC detect](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/GrpcKa/grpc-detect-module.go): Probe engine · Low-level gRPC stream detection and reporting support · `cmd/lib/GrpcKa/grpc-detect-module.go`

### Integrations

External outputs and adjacencies

- [InfluxDB](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/InfluxDB/influxdb-util.go): Time-series util · Persistence and metrics utility layer for historical monitoring data · `cmd/lib/InfluxDB/influxdb-util.go`
- [Email](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/email-module.go): Alerting module · SMTP notification path for alert delivery · `cmd/api/email-module.go`
- [WeChat](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/wechat-module.go): Alerting module · WeChat notification integration for monitoring events · `cmd/api/wechat-module.go`
- [Gateway](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/gateway-module.go): Device integration · TP-Link gateway control and SOAP-based device interaction · `cmd/api/gateway-module.go`
- [Topology](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/topo-module.go): Topology integration · Topology and network-graph integration boundary · `cmd/api/topo-module.go`
- [Cloud monitor](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/api/mao-cloud-monitor-wrapper-module.go): Wrapper · Wrapper for cloud monitoring integration and side effects · `cmd/api/mao-cloud-monitor-wrapper-module.go`

### Shared Libs

Common utilities and contracts

- [Registry](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/MaoCommon/service-registry.go): Shared utility · Service registry helpers shared across client, server, and integrations · `cmd/lib/MaoCommon/service-registry.go`
- [Restful lib](https://github.com/maojianwei/mao-service-discovery/blob/master/cmd/lib/Restful/restful-server-impl.go): HTTP utility · Reusable server implementation for the REST layer · `cmd/lib/Restful/restful-server-impl.go`
- [Proto API](https://github.com/maojianwei/mao-service-discovery/blob/master/grpc.maojianwei.com/server/discovery/api/mao-server-discovery.proto): gRPC contract · Protobuf schema and generated service bindings for discovery transport · `grpc.maojianwei.com/server/discovery/api/mao-server-discovery.proto`
- [Util](https://github.com/maojianwei/mao-service-discovery/blob/master/util/mao_util.go): Helpers · Logging and general-purpose helpers used across the repository · `util/mao_util.go`

### WebUI

Vue frontend and served assets

- [WebUI](https://github.com/maojianwei/mao-service-discovery/blob/master/webui/src/App.vue): Vue 3 app · Standalone frontend for server status, config, ICMP, gRPC, topology, email, and MySQL views · `webui/src/App.vue`
- [Router](https://github.com/maojianwei/mao-service-discovery/blob/master/webui/src/mao-router.js): Frontend routing · Client-side route map and page navigation for the WebUI · `webui/src/mao-router.js`
- [Static assets](https://github.com/maojianwei/mao-service-discovery/tree/master/resource/static): Built UI · Compiled frontend assets served by the backend at runtime · `resource/static`
- [HTML templates](https://github.com/maojianwei/mao-service-discovery/blob/master/resource/html/index.html): Templates · Server-rendered HTML shells and entry pages · `resource/html/index.html`

### Packaging

Build and release artifacts

- [Release pack](https://github.com/maojianwei/mao-service-discovery/tree/master/Dockerfile): Packaging · Build scripts and release manifests for Docker and platform-specific binaries · `Dockerfile`

### Experimental

Archived or non-core implementations

- [Incubator](https://github.com/maojianwei/mao-service-discovery/tree/master/incubator): Experimental · Non-core and exploratory integrations kept outside the main runtime path · `incubator`
- [Backup code](https://github.com/maojianwei/mao-service-discovery/tree/master/bak): Archived · Historical and backup implementations, tests, and older experiments · `bak`

## Connections

- Main → Server cmd: server mode · Bootstraps the server runtime
- Main → Client mode: client mode · Bootstraps the client reporting runtime
- Main → Client v2: client v2 · Alternative client entrypoint
- Server cmd → API core: orchestrates · Hands server execution to the API orchestration layer
- Client mode → gRPC detect: reports via · Sends discovery data over the gRPC client path
- Client v2 → gRPC detect: reports via · Alternative client path using gRPC transport
- API core → Config: loads · Applies runtime configuration
- API core → ICMP module: wires · Connects ICMP monitoring into server orchestration
- API core → gRPC module: wires · Connects gRPC reporting into server orchestration
- API core → REST API: exposes · Publishes server state through HTTP/JSON
- API core → Aux data: enriches · Adds auxiliary data collection and processing
- API core → Email: alerts · Routes notifications to email delivery
- API core → WeChat: alerts · Routes notifications to WeChat delivery
- API core → Gateway: integrates · Connects gateway control and device interaction
- API core → Topology: integrates · Connects topology state and visualization data
- API core → Cloud monitor: integrates · Connects cloud monitor side effects
- ICMP module → ICMP detect: uses · Server wrapper around the ICMP engine
- gRPC module → gRPC detect: uses · Server wrapper around the gRPC engine
- REST API → Restful lib: implements · Built on the reusable REST server implementation
- REST API → WebUI: serves · Provides API responses consumed by the WebUI
- REST API → Static assets: serves · Serves compiled frontend assets
- REST API → HTML templates: renders · Uses HTML entry templates for the UI shell
- API core → InfluxDB: persists · Writes or reads historical monitoring metrics
- API core → Registry: shares · Uses registry utilities for common discovery coordination
- API core → Proto API: contracts · Relies on the discovery protobuf contract and generated bindings
- API core → Util: logs/util · Uses shared logging and helper functions
- WebUI → Router: navigates · Routes between dashboard and config pages
- Release pack → Static assets: bundles · Packages compiled UI assets with the backend runtime
- Release pack → WebUI: builds · Builds the separate frontend application
- Release pack → Main: ships · Delivers the Go backend binary in multiple distribution formats
- Incubator → API core: adjacent · Contains exploratory integrations around the same domain
- Backup code → ICMP module: history · Archived experiments and older ICMP-related implementations
