# liuguangw/billing_go architecture

> Architecture diagram of the GitHub repository liuguangw/billing_go, 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/liuguangw/billing_go
- Repository: https://github.com/liuguangw/billing_go

## Overview

##### Main Components
- Billing Server Binary: Represent the main executable (billing on Linux and billing.exe for Windows) that is started via main.go.
- Command/CLI Interface: The cmd/ folder contains commands (app_command.go, up, stop, version) that control server behavior (e.g., launching servers in daemon vs. foreground mode, stopping the server).
- Business Handlers: The bhandler/ package holds various handler implementations (such as login_handler, register_handler, enter_game_handler, etc.) that process different billing or game-specific commands.
- Data Models: The models/ directory contains definitions for handling accounts, login checks, and point conversion logic.
- Core Services:
- Billing Service: Located under services/billing; this module initializes the database, sets up listeners, configures logging, handles incoming billing packets, and manages user queries (e.g., show_users).
- Connection Handling: The services/handle package is responsible for managing low-level network tasks such as accepting connections, reading/writing packets, and enforcing policies (e.g., allowed addresses).
- Utility & Configuration Services: Other services (like load_server_config.go, packet_data_reader.go, process_attr_xxx.go, run_background.go, show_version_info.go) support system configuration, platform-specific functionalities, and run-time background tasks.
- External Components:
- Login Server: Since the README describes that only the “Login” server connects to billing, represent this external dependency.
- MySQL Database: The configuration file (config.yaml) indicates that billing communicates with a MySQL instance for account and related data.
- Log Files: Although not a software component per se, the billing log and Login server’s logs are critical for debugging and monitoring.

##### Relationships & Data Flow
- Flow from handlers (in bhandler/) to models – for instance, upon processing a login or registration request, business logic within models is called.
- Optionally, include the command-line control path: commands in cmd/ (up, stop, version) that send signals to the billing service.

##### Architectural Patterns & Design Principles
- Layered Architecture: Separate concerns into presentation (CLI via cmd/), application logic (handlers in bhandler and models), and infrastructural services (services for networking, logging, and configuration).
- Modular Organization: Different packages (bhandler, models, services, common) isolate functionality, which supports easier maintenance and potential scalability.
- Platform-Specific Handling: With files like process_attr_linux.go and process_attr_windows.go, indicate that the system contains specialized modules to handle cross-platform differences.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Layer
    subgraph "Entry Layer"
        A["Command/CLI Interface (cmd/)"]:::cli
        B["Main Server (main.go)"]:::entry
    end

    %% Core Infrastructure Layer
    subgraph "Core Infrastructure"
        C["Billing Service (services/billing/)"]:::infra
        G["Configuration Loader (services/load_server_config.go)"]:::infra
        D["Connection Handler (services/handle/)"]:::infra
        J["Utility & Config Services"]:::infra
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        E["Business Handlers (bhandler/)"]:::biz
        F["Data Models (models/)"]:::biz
    end

    %% External Systems
    subgraph "External Systems"
        H["Login Server"]:::external
        I["MySQL Database"]:::external
        K["config.yaml"]:::external
    end

    %% Connections
    A -->|"initiates"| B
    B -->|"starts"| C
    K -->|"configuration"| G
    G -->|"loads_config"| C
    C -->|"manages_connections"| D
    H -->|"sends_packets"| D
    C -->|"delegates_logic"| E
    E -->|"invokes_models"| F
    J -->|"supports"| C
    C -->|"queries_updates"| I

    %% Click Events
    click A "https://github.com/liuguangw/billing_go/tree/master/cmd/"
    click B "https://github.com/liuguangw/billing_go/blob/master/main.go"
    click C "https://github.com/liuguangw/billing_go/tree/master/services/billing/"
    click D "https://github.com/liuguangw/billing_go/tree/master/services/handle/"
    click E "https://github.com/liuguangw/billing_go/tree/master/bhandler/"
    click F "https://github.com/liuguangw/billing_go/tree/master/models/"
    click G "https://github.com/liuguangw/billing_go/blob/master/services/load_server_config.go"
    click K "https://github.com/liuguangw/billing_go/blob/master/config.yaml"

    %% Styles
    classDef cli fill:#DDEEFF,stroke:#000,stroke-width:1px;
    classDef entry fill:#BBCCFF,stroke:#000,stroke-width:1px;
    classDef infra fill:#FFDDCC,stroke:#000,stroke-width:1px;
    classDef biz fill:#CCFFCC,stroke:#000,stroke-width:1px;
    classDef external fill:#FFCCCC,stroke:#000,stroke-width:1px;
```
