How nezhahq/nezha works
Main Components
• Dashboard Frontend:
• There are two separate “dist” directories under cmd/dashboard (admin-dist and user-dist) which contain the static assets for the admin and user interfaces.
• Backend API/Controller Layer:
• Located under cmd/dashboard/controller, this layer contains many Go files (e.g., alertrule.go, jwt.go, oauth2.go, etc.) exposing RESTful endpoints.
• RPC and gRPC Services:
• In addition to HTTP controllers, there is an RPC folder (under cmd/dashboard/rpc and service/rpc) to handle remote procedure calls.
• The proto folder shows Protobuf definitions (nezha.proto) and generated code; these support gRPC-based communications.
• Service/Singleton Components:
• The service/singleton directory includes background tasks (e.g., for DDNS updates, scheduled tasks, alert sending, etc.) that run concurrently.
• Data Model and Utility Libraries:
• The model folder holds the domain models used by the application, including API and test files.
• Libraries under pkg (ddns, geoip, i18n, utils, websocketx) support functionalities such as dynamic DNS, internationalization, and HTTP/request wrapping.
• Development and Deployment:
• Files like Dockerfile, .devcontainer, and various GitHub workflows indicate containerization and CI/CD.
• Scripts (in script/) manage bootstrap, configuration, and frontend fetching.
• Configuration & Internationalization:
• The service/singleton/frontend-templates.yaml and pkg/i18n indicate theming and language support.
Relationships and Data Flows
• HTTP Clients (Admin & User Frontends) interact with the Backend API:
• Requests hit controllers in cmd/dashboard/controller.
• Controllers call services (such as those in service/singleton for scheduled tasks or state management) and may involve RPC calls.
• gRPC / RPC Communication:
• Some components use defined gRPC endpoints (illustrated by protobuf files and the pkg/grpcx) to communicate with other services or internal modules.
• Utility and Infrastructure services support core functionalities:
• Packages like pkg/ddns or pkg/geoip are invoked by controllers or services for specific features.
• Deployment and Containerization:
• Container configuration (.devcontainer, Dockerfile) and CI/CD workflows help manage build, test, and release pipelines.
Architectural Patterns and Design Principles
• Layered Architecture:
• The project separates concerns among presentation (Frontend distributions), business logic (Controllers and Service/Singleton modules), and data models (Model package).
• Microservices or Modular RPC:
• While it’s a monorepo, distinct folders for RPC and singleton services suggest modular internal services interacting via well-defined interfaces.
• Use of Utility Libraries:
• The pkg subdirectories encapsulate cross-cutting concerns (e.g., internationalization, ddns, utils).
• Separation of Concerns:
• Controllers manage HTTP routing while backend tasks (e.g., scheduling, notification) are handled by service layers.
Relevant Technologies, Frameworks, and Libraries
• Go Language:
• The entire code base is written in Go (as seen with go.mod and go.sum).
• gRPC/Protocol Buffers:
• The presence of proto files and generated code indicate gRPC communication.
• Containerization:
• Dockerfile and .devcontainer files suggest development in containerized environments.
• CI/CD Integration:
• GitHub workflows for testing, code analysis, releases, and synchronization.
• Internationalization:
• pkg/i18n shows support for multiple languages with translation files in various locales.