# nezhahq/nezha architecture

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

- Interactive diagram: https://gitdiagram.com/nezhahq/nezha
- Repository: https://github.com/nezhahq/nezha

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Clients
    Clients("Clients/Users"):::clients

    %% Frontend Layer
    subgraph "Frontend Layer"
        FE_A("Dashboard Frontend (Admin UI)"):::frontend
        FE_U("Dashboard Frontend (User UI)"):::frontend
    end

    %% Connections from Clients to Frontend
    Clients -->|"HTTP-Req"| FE_A
    Clients -->|"HTTP-Req"| FE_U

    %% Backend Layer
    subgraph "Backend Layer"
        Controller("API Controllers - REST"):::controller
        Service("Background Services"):::service
        subgraph "RPC Services"
            RPC_HTTP("HTTP RPC"):::rpc
            RPC_gRPC("gRPC & Protobuf"):::rpc
        end
        %% Relationships within Backend Layer
        Controller -->|"calls"| Service
        Controller -->|"RPC-call"| RPC_HTTP
        Controller -->|"RPC-call"| RPC_gRPC
        Service -->|"RPC-comm"| RPC_HTTP
        Service -->|"RPC-comm"| RPC_gRPC
    end

    %% Utility & Data Model Layer
    subgraph "Utility & Data Model"
        DM("Data Models"):::model
        subgraph "Utility Libraries"
            U_ddns("DDNS"):::utility
            U_geoip("GeoIP"):::utility
            U_i18n("Internationalization"):::utility
            U_utils("Utils"):::utility
            U_ws("Websockets"):::utility
        end
    end

    %% Connections from Backend Layer to Utility Components
    Controller -->|"uses"| U_ddns
    Controller -->|"uses"| U_geoip
    Controller -->|"uses"| U_i18n
    Controller -->|"uses"| U_utils
    Controller -->|"uses"| U_ws
    Service -->|"uses"| U_ddns
    Service -->|"uses"| U_geoip
    Service -->|"uses"| U_i18n
    Service -->|"uses"| U_utils
    Service -->|"uses"| U_ws

    %% DevOps & Deployment
    subgraph "DevOps & Deployment"
        DevCont(".devcontainer"):::devops
        Docker("Dockerfile"):::devops
        GitHub(".github Workflows"):::devops
        Scripts("script"):::devops
        Config("Config & I18N"):::devops
    end

    %% Support arrows from DevOps to Backend (dotted to denote environment support)
    DevCont -.-> Controller
    Docker -.-> Controller
    GitHub -.-> Controller
    Scripts -.-> Controller
    Config -.-> U_i18n

    %% Connections from Frontend to Backend
    FE_A -->|"HTTP-Req"| Controller
    FE_U -->|"HTTP-Req"| Controller

    %% Styling Classes
    classDef clients fill:#FFF2CC,stroke:#000,stroke-width:1px;
    classDef frontend fill:#A3C2F2,stroke:#000,stroke-width:1px;
    classDef controller fill:#A3F2A3,stroke:#000,stroke-width:1px;
    classDef service fill:#F2C2A3,stroke:#000,stroke-width:1px;
    classDef rpc fill:#D4A5F5,stroke:#000,stroke-width:1px;
    classDef model fill:#AAAAAA,stroke:#000,stroke-width:1px;
    classDef utility fill:#CCCCCC,stroke:#000,stroke-width:1px;
    classDef devops fill:#F5E6A3,stroke:#000,stroke-width:1px;

    %% Click Events
    click FE_A "https://github.com/nezhahq/nezha/tree/master/cmd/dashboard/admin-dist"
    click FE_U "https://github.com/nezhahq/nezha/tree/master/cmd/dashboard/user-dist"
    click Controller "https://github.com/nezhahq/nezha/tree/master/cmd/dashboard/controller"
    click RPC_HTTP "https://github.com/nezhahq/nezha/tree/master/cmd/dashboard/rpc"
    click RPC_gRPC "https://github.com/nezhahq/nezha/tree/master/proto"
    click Service "https://github.com/nezhahq/nezha/tree/master/service/singleton"
    click DM "https://github.com/nezhahq/nezha/tree/master/model"
    click U_ddns "https://github.com/nezhahq/nezha/tree/master/pkg/ddns"
    click U_geoip "https://github.com/nezhahq/nezha/tree/master/pkg/geoip"
    click U_i18n "https://github.com/nezhahq/nezha/tree/master/pkg/i18n"
    click U_utils "https://github.com/nezhahq/nezha/tree/master/pkg/utils"
    click U_ws "https://github.com/nezhahq/nezha/tree/master/pkg/websocketx"
    click DevCont "https://github.com/nezhahq/nezha/blob/master/.devcontainer"
    click Docker "https://github.com/nezhahq/nezha/tree/master/Dockerfile"
    click GitHub "https://github.com/nezhahq/nezha/blob/master/.github"
    click Scripts "https://github.com/nezhahq/nezha/tree/master/script"
    click Config "https://github.com/nezhahq/nezha/blob/master/service/singleton/frontend-templates.yaml"
```
