How maojianwei/mao-service-discovery works
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: api API core · restful-server-module.go REST API · main.go Main · Dockerfile Release pack
Components 29
Runtime
Go entrypoints and mode selection
- Main · Process bootstrap and handoff into the mode-driven runtime
main.go - Server cmd · Server-mode entrypoint that aggregates monitoring data and serves APIs
cmd/server.go - Client mode · Client-mode entrypoint for reporting discovery data
cmd/general_client.go - Client v2 · Alternative client entrypoint for the discovery/reporting path
cmd/general_client_v2.go
Core API
Server orchestration and domain-facing modules
- API core · Central wiring layer that combines config, discovery, storage, notifications, and UI-facing services
cmd/api - Config · Configuration loading and normalization for runtime behavior
cmd/api/config-module.go - REST API · JSON/HTTP service boundary used by the WebUI and other consumers
cmd/api/restful-server-module.go - Aux data · Secondary data collection and enrichment pipeline
cmd/api/aux-data-module.go
Signals
Discovery and keepalive mechanisms
- ICMP module · Server-side wrapper around ICMP keepalive/detection
cmd/api/icmp-ka-module.go - gRPC module · Server-side wrapper around gRPC-based client reporting
cmd/api/grpc-ka-module.go - ICMP detect · Low-level ICMP reachability and heartbeat detection
cmd/lib/IcmpKa/icmp-detect-module.go - gRPC detect · Low-level gRPC stream detection and reporting support
cmd/lib/GrpcKa/grpc-detect-module.go
Integrations
External outputs and adjacencies
- InfluxDB · Persistence and metrics utility layer for historical monitoring data
cmd/lib/InfluxDB/influxdb-util.go - Email · SMTP notification path for alert delivery
cmd/api/email-module.go - WeChat · WeChat notification integration for monitoring events
cmd/api/wechat-module.go - Gateway · TP-Link gateway control and SOAP-based device interaction
cmd/api/gateway-module.go - Topology · Topology and network-graph integration boundary
cmd/api/topo-module.go - Cloud monitor · Wrapper for cloud monitoring integration and side effects
cmd/api/mao-cloud-monitor-wrapper-module.go
Shared Libs
Common utilities and contracts
- Registry · Service registry helpers shared across client, server, and integrations
cmd/lib/MaoCommon/service-registry.go - Restful lib · Reusable server implementation for the REST layer
cmd/lib/Restful/restful-server-impl.go - Proto API · Protobuf schema and generated service bindings for discovery transport
grpc.maojianwei.com/server/discovery/api/mao-server-discovery.proto - Util · Logging and general-purpose helpers used across the repository
util/mao_util.go
WebUI
Vue frontend and served assets
- WebUI · Standalone frontend for server status, config, ICMP, gRPC, topology, email, and MySQL views
webui/src/App.vue - Router · Client-side route map and page navigation for the WebUI
webui/src/mao-router.js - Static assets · Compiled frontend assets served by the backend at runtime
resource/static - HTML templates · Server-rendered HTML shells and entry pages
resource/html/index.html
Packaging
Build and release artifacts
- Release pack · Build scripts and release manifests for Docker and platform-specific binaries
Dockerfile
Experimental
Archived or non-core implementations
- Incubator · Non-core and exploratory integrations kept outside the main runtime path
incubator - Backup code · Historical and backup implementations, tests, and older experiments
bak
Connections 32
- 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