# haomiao000/dy architecture

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

- Interactive diagram: https://gitdiagram.com/haomiao000/dy
- Repository: https://github.com/haomiao000/dy

## Overview

##### Project Type and Purpose
- The project is clearly a backend microservices system built using Go, with a focus on different business capabilities such as user management, video handling, interactions (comments, favorites), and social relations.
- The presence of directories like “server/base_serv” (with submodules like interact, relation, user, video), “server/gateway_serv”, and “server/redis_svr” indicates a microservices setup where a gateway routes API requests to dedicated service modules.
- The README’s architecture design image (although external) and the mention of multiple “接口文档” (API documentation), “工具类文档” (tool documentation), etc., further confirm that this is a distributed system built with modularity and clear component separation in mind.

##### Analyzing the File Structure
- Key directories and components:
- “comm” directory: Contains shared modules for discovery (likely service discovery), Redis interactions, and distributed tracing.
- “internal” directory: Houses gRPC-generated code and interceptor logic which are vital for implementing RPC communication and middleware functions.
- “server” directory: Divided into different service modules:
▪ “base_serv”: Contains multiple subfolders for core business logic like interact, relation, user, and video. Each has its own API server, configuration, DAO (data access), initialization scripts, and gRPC definition files.
▪ “gateway_serv”: Likely acts as the API gateway, handling the routing, authentication (using middleware like JWT), and aggregating responses from underlying services.
▪ “redis_svr”: A dedicated Redis service providing caching or other Redis-based operations.
- “scripts” & “build”: Contain Dockerfiles and utility scripts (like proto generation, db operations, etc.) for building and deploying the system.
- “assets”: May hold static content used (like videos), possibly supporting a minor frontend or media service.
- Configuration files, build scripts, and deployment files (e.g., “dockerfile” under build/deploy, “grpc.yaml” files) indicate that the system is containerized and uses protocol definitions for interservice communication.

a. Main Components:
- API Gateway: Represented by “server/gateway_serv”. This acts as the single entry point for client requests.
- Service Modules (“base_serv”): Break these into four main business services:
- Interact Service (handles comments, favorites, etc.)
- Relation Service (manages following/follower relationships)
- User Service (user registration, management, login, etc.)
- Video Service (video upload, retrieval, and management)
- Redis Service: Represented by “server/redis_svr”, used either for caching or as a fast in-memory data store.
- Common/Shared Libraries: “comm” and “server/common” which provide cross-cutting concerns like service discovery (possibly backed by etcd as seen in configuration files), distributed tracing (with Jaeger integration), and authentication middleware (JWT).
- gRPC & Protocol Modules: “internal/grpc_gen” and “scripts/proto” illustrate how RPC communications are structured between services.
- Infrastructure & Operations: Deployment scripts, Docker configurations, and build scripts show the DevOps aspect of the system.

b. Relationships and Interactions:
- The API Gateway receives external API requests. It then routes these request using business logic either directly or via RPC calls to the respective base_serv modules.
- The base_serv modules (user, video, interact, relation) can interact with one another via gRPC, as indicated by the generated gRPC code and the “grpc.yaml” files.
- Common middleware (from “server/common” and “internal/interceptor”) is applied for logging, authentication, and tracing (Jaeger and JWT).
- Shared components (like Redis in “server/redis_svr”) may be called by multiple services to cache data or manage sessions.
- The “comm” modules provide auxiliary functionalities such as service discovery (ensuring dynamic routing and addressing), trace logging, and Redis connectivity across services.

c. Architectural Patterns and Technologies:
- Microservices Architecture: The separation of services (user, video, interact, relation, gateway) indicates independent deployment, scalability, and intercommunication.
- RPC Communication: Extensive use of gRPC for service-to-service calls.
- Middleware Pattern: Use of interceptors for cross-cutting concerns.
- Containerization: Docker-related files indicate that the system is containerized for deployment.
- Use of configuration management (etcd configurations) for dynamic service discovery or configuration updates.
- Technologies: Go for implementation, MySQL (as seen in “dao/mysql.go”) for data persistence, Redis for caching, Jaeger for distributed tracing, and etcd for service discovery/configuration.

- For a microservices-based backend, focus on clearly delineating service boundaries between the API Gateway, business modules, and supporting services like Redis.
▪ Another for business service modules (Interact, Relation, User, Video).
▪ A third for supporting/common utilities (discovery, tracing, middleware).

##### Elements
- Clear Labels: Each component should have a name (e.g., “API Gateway”, “User Service”, “Relational Service”, “Redis Service”, “Discovery Module”, “Tracing Module”).
- Entry point components (API Gateway)
- Business logic modules (microservices under base_serv)
- Infrastructure services (Redis, common libraries, service discovery, tracing)
- Utility or support sections (build, scripts, test)

- A bidirectional flow between the API Gateway and the servers using gRPC.
- Outside these, include a “Common Services” block representing “comm” and “server/common” that interact with almost every service (for tasks such as service discovery and distributed tracing).
- A “Deployment & DevOps” section to denote Docker build/execution pipelines and overall container orchestration.
- Finally, overlay middleware functions as a layer that spans API Gateway and inter-service communication, annotating that they incorporate JWT, Jaeger, etc.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Level
    Client("Client"):::client
    Client -->|"HTTP"| APIGateway("API Gateway"):::api

    %% Middleware Layer
    APIGateway -->|"JWT,Jaeger"| MiddlewareA("Server Middleware"):::middleware
    APIGateway -->|"Interceptor"| GRPCInterceptor("gRPC Interceptor"):::middleware

    %% gRPC Module
    MiddlewareA -->|"gRPC Forwarding"| GRPC("gRPC & Protocol Modules"):::grpc
    GRPCInterceptor -.-> GRPC

    %% Business Services Subgraph
    subgraph "Business Services"
        direction TB
        Interact("Interact Service"):::service
        Relation("Relation Service"):::service
        User("User Service"):::service
        Video("Video Service"):::service
    end

    %% Connections from gRPC Module to Business Services
    GRPC -->|"gRPC call"| Interact
    GRPC -->|"gRPC call"| Relation
    GRPC -->|"gRPC call"| User
    GRPC -->|"gRPC call"| Video

    %% Redis Service
    Redis("Redis Service"):::db

    %% Each Business Service uses Redis for caching/state management
    Interact -->|"Cache"| Redis
    Relation -->|"Cache"| Redis
    User -->|"Cache"| Redis
    Video -->|"Cache"| Redis

    %% Common Services Subgraph
    subgraph "Common Services"
        direction TB
        Discovery("Discovery Module"):::common
        CommonRedis("Redis Library"):::common
        Tracing("Tracing Module"):::common
    end

    %% gRPC Module utilizes common services for service discovery and tracing
    GRPC -->|"UsesDiscovery"| Discovery
    GRPC -->|"UsesTracing"| Tracing

    %% Class Definitions for Color Coding
    classDef client fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef api fill:#ABEBC6,stroke:#2ECC71,stroke-width:2px;
    classDef middleware fill:#FAD7A0,stroke:#F39C12,stroke-width:2px;
    classDef grpc fill:#D6EAF8,stroke:#3498DB,stroke-width:2px;
    classDef service fill:#F5B7B1,stroke:#E74C3C,stroke-width:2px;
    classDef db fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef common fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;

    %% Click Events
    click APIGateway "https://github.com/haomiao000/dy/tree/main/server/gateway_serv/gateway"
    click Interact "https://github.com/haomiao000/dy/tree/main/server/base_serv/interact"
    click Relation "https://github.com/haomiao000/dy/tree/main/server/base_serv/relation"
    click User "https://github.com/haomiao000/dy/tree/main/server/base_serv/user"
    click Video "https://github.com/haomiao000/dy/tree/main/server/base_serv/video"
    click Redis "https://github.com/haomiao000/dy/tree/main/server/redis_svr"
    click Discovery "https://github.com/haomiao000/dy/tree/main/comm/discovery"
    click CommonRedis "https://github.com/haomiao000/dy/tree/main/comm/redis"
    click Tracing "https://github.com/haomiao000/dy/tree/main/comm/trace"
    click GRPC "https://github.com/haomiao000/dy/tree/main/internal/grpc_gen"
    click MiddlewareA "https://github.com/haomiao000/dy/tree/main/server/common/middleware"
    click GRPCInterceptor "https://github.com/haomiao000/dy/tree/main/internal/interceptor"
```
