# zihuangning/gogameserver architecture

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

- Interactive diagram: https://gitdiagram.com/zihuangning/gogameserver
- Repository: https://github.com/zihuangning/gogameserver

## Overview

##### Project Purpose and Type
- This is a game server project built in Go. Its purpose is to support an online game with various services (chat, game, login, API, etc.).
- The README indicates the use of Consul for service registration/discovery, protocol buffers for client communication, and multiple underlying data stores (MySQL, MongoDB, Redis). Services are built using a microservices-like architecture with TCP-based IPC and RPC channels.

##### Analyzing the File Structure
- Top-level directories include:
- config: Contains local configuration files (for MySQL, MongoDB, Redis, Consul, etc.).
- core: Contains the internal libraries and utilities—configuration handling, logging, database connectors, GRPC and RPC, JWT, session management, and other support libraries—all of which are common to many services.
- servives (likely meant to be “services”): Contains modular subdirectories for specific game server functions such as api, chat, connector, game, log, and login. Each directory has its own main entry point and additional submodules (controllers, models, cache, etc.) that implement service-specific logic.
- dbs: Holds database schema files (e.g., user.sql).
- scripts: Shell scripts for building, running, stopping, configuring, and generating protobuf files.
- Others: Test scripts and files indicating testing support (e.g., test/ directory).

##### Key Architectural Components
a. Service Registry & Communication Backbone:
- Consul: Highlight that Consul is used for service registration/discovery (config files present, and the core/libs/consul module).
- Communication protocols: Use of gRPC (see core/libs/grpc), IPC for inter-process communication using protobuf (tracked under core/messages, core/libs/grpc/ipc).

b. Core Libraries and Infrastructure:
- core: This layer abstracts common functionality such as configuration parsing, logging, session management, JWT handling, and database connections for MySQL, MongoDB, and Redis.
- Each of these libraries forms the basis of inter-service communication, error handling, and utility functions.

c. Microservice Components:
- API Service (servives/api): Acts as the front-facing application interface handling client requests.
- Chat Service (servives/chat): Handles chat-specific functionalities, including caching of online user data.
- Connector Service (servives/connector): Likely manages the connections between clients and backend services, complete with message processors and caching.
- Game Service (servives/game): Manages game-specific logic and user modules.
- Login Service (servives/login): Handles user authentication—caching, online sessions, and login routines.
- Log Service (servives/log): Dedicated to logging operations in the system.
- Public Shared Modules (servives/public): Contains shared code (e.g., error codes, proto definitions, Mongo/MySQL/Redis instance setups) that can be used by multiple services.
- Test Service/Entry: Used for testing functionality (found in servives/test and test directories).

d. Data Storage Components:
- Databases: MySQL and MongoDB databases (referenced in config files and core service modules).
- Caching: Redis is used as a cache layer, and some services (such as chat and login) include caching modules.

e. Protocols & Integration:
- Protocol Buffers: Used for client communications (with proto definitions in servives/public/gameProto and GRPC modules in core).
- TCP-based IPC, RPC: Services use these for inter-service interactions.
- Websocket/Socket: Parts of the code (in core/libs/socket and core/libs/websocket) may indicate real-time communication especially useful in gaming applications.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    A["External Clients"]:::external

    %% Service Registry Subgraph
    subgraph "Service Registry"
        R1["Consul (Core Implementation)"]:::registry
        R2["Consul (Configuration)"]:::registry
    end

    %% gRPC & IPC Communication Subgraph
    subgraph "gRPC & IPC Communication"
        G1["gRPC Core"]:::grpc
        G2["IPC Modules"]:::grpc
    end

    %% Core Libraries & Database Connectors Subgraph
    subgraph "Core & Infrastructure"
        C1["Core Libraries & Utilities"]:::core
        subgraph "Database & Cache Connectors"
            D1["MySQL Connector"]:::core
            D2["MongoDB Connector"]:::core
            D3["Redis Connector"]:::core
        end
    end

    %% Microservices Subgraph
    subgraph "Microservices"
        M1["API Service"]:::service
        M2["Chat Service"]:::service
        M3["Connector Service"]:::service
        M4["Game Service"]:::service
        M5["Login Service"]:::service
        M6["Log Service"]:::service
        M7["Public Shared Modules"]:::service
        M8["Test Service"]:::service
        M9["Additional Tests"]:::service
    end

    %% Data Stores Subgraph
    subgraph "Data Stores"
        DB1["((MySQL))"]:::db
        DB2["((MongoDB))"]:::db
        DB3["((Redis))"]:::db
    end

    %% Connections from External Clients to API Service
    A -->|"protobuf"| M1

    %% Each microservice registers with Consul (Service Registry)
    M1 -->|"register"| R1
    M2 -->|"register"| R1
    M3 -->|"register"| R1
    M4 -->|"register"| R1
    M5 -->|"register"| R1
    M6 -->|"register"| R1
    M7 -->|"register"| R1
    M8 -->|"register"| R1
    M9 -->|"register"| R1

    %% Inter-service communications
    M1 -->|"auth"| M5
    M3 -->|"IPC"| M2
    M3 -->|"IPC"| M4

    %% Microservices dependency on Core Libraries
    M1 --> C1
    M2 --> C1
    M3 --> C1
    M4 --> C1
    M5 --> C1
    M6 --> C1
    M7 --> C1
    M8 --> C1
    M9 --> C1

    %% Core Libraries dependency on gRPC & IPC Communication
    C1 --> G1
    G1 --> G2

    %% Core Libraries providing Database/Cache access via connectors
    C1 --> D1
    C1 --> D2
    C1 --> D3

    %% Database Connectors connecting to Data Stores
    D1 --> DB1
    D2 --> DB2
    D3 --> DB3

    %% Click Events
    click R1 "https://github.com/zihuangning/gogameserver/tree/master/core/libs/consul"
    click R2 "https://github.com/zihuangning/gogameserver/blob/master/config/local/service.json"
    click G1 "https://github.com/zihuangning/gogameserver/tree/master/core/libs/grpc"
    click G2 "https://github.com/zihuangning/gogameserver/tree/master/core/libs/grpc/ipc"
    click C1 "https://github.com/zihuangning/gogameserver/tree/master/core"
    click D1 "https://github.com/zihuangning/gogameserver/blob/master/core/service/service_mysql.go"
    click D2 "https://github.com/zihuangning/gogameserver/blob/master/core/service/service_mongo.go"
    click D3 "https://github.com/zihuangning/gogameserver/blob/master/core/service/service_redis.go"
    click M1 "https://github.com/zihuangning/gogameserver/tree/master/servives/api"
    click M2 "https://github.com/zihuangning/gogameserver/tree/master/servives/chat"
    click M3 "https://github.com/zihuangning/gogameserver/tree/master/servives/connector"
    click M4 "https://github.com/zihuangning/gogameserver/tree/master/servives/game"
    click M5 "https://github.com/zihuangning/gogameserver/tree/master/servives/login"
    click M6 "https://github.com/zihuangning/gogameserver/tree/master/servives/log"
    click M7 "https://github.com/zihuangning/gogameserver/tree/master/servives/public"
    click M8 "https://github.com/zihuangning/gogameserver/tree/master/servives/test"
    click M9 "https://github.com/zihuangning/gogameserver/tree/master/test"

    %% Styles
    classDef external fill:#FFE599,stroke:#B45F06,stroke-width:2px;
    classDef registry fill:#C9E8C6,stroke:#6AA84F,stroke-width:2px;
    classDef grpc fill:#F4CCCC,stroke:#CC0000,stroke-width:2px;
    classDef core fill:#D9EAD3,stroke:#6AA84F,stroke-width:2px;
    classDef service fill:#C9DAF8,stroke:#1155CC,stroke-width:2px;
    classDef db fill:#FCE5CD,stroke:#E69138,stroke-width:2px;
```
