# zhaojh329/rttys architecture

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

- Interactive diagram: https://gitdiagram.com/zhaojh329/rttys
- Repository: https://github.com/zhaojh329/rttys

## Overview

##### Project Type & Overall Purpose
- The project is a backend server application (written in Go) that implements the server counterpart of rtty, managing device connections, authentication (via tokens or mTLS), database interactions, logging, and client (or device) communications.
- It also includes a dedicated frontend (in the “ui” directory) that is built with Node.js/Vue (using Vite) and provides a web interface for management and interaction.

##### Main Components
A. Backend (Golang Server)
- Core Server: Centered around main.go, which initializes and orchestrates the system.
- API/HTTP Layer: Represented by files like api.go and http.go, which likely expose HTTP endpoints and handle incoming requests.
- Authentication Module: Include the token generation logic (command.go, possibly with token handling capabilities) and mTLS support (enabled via configuration and CA files handled in config/config.go).
- Broker & Device Services: broker.go and device.go indicate that the system handles real-time device connections and communications, acting as a broker between devices and clients.
- Database Integration: Database connectivity is managed via database.go, with support for several database systems (SQLite, MySQL/Mariadb) as described in the README.
- Configuration and Logging: config (config/config.go) and log (log/log.go) directories manage system settings and runtime logs.
- Utility Functions: The utils directory (utils/utils.go) provides helper and shared functionality.
- Versioning: The version/version.go file indicates that version management is built into the application.
- Client Module: The “client” directory, with client/client.go, supports connections or interactions from client devices.
- Caching: The cache directory (cache/cache.go) suggests some caching mechanism is in place.

B. Frontend (UI)
- UI Application: Located in the “ui” folder, built with Vue. Key files include:
– Index File: ui/index.html as the entry point.
– App & Components: ui/src/App.vue, ui/src/components (e.g., ContextMenu.vue), establishing the UI’s component structure.
– Routing & State Management: ui/src/router and i18n directories manage routes and internationalization.
– Additional Enhancements: Integration with xterm via “xterm-addon” for terminal emulation.
– Build Configuration: ui/vite.config.js, package.json, and npm scripts indicate a modern frontend build process.

C. Deployment & External Components
- Docker: Dockerfile and build.sh scripts illustrate that the backend can be containerized.
- Nginx Proxy: The README includes sample configuration files (rttys.conf and nginx.conf) that show how nginx is used to proxy connections (separately handling HTTP proxy and device connections).
- External Authentication: The README outlines how the system interacts with an external authentication server (for device authorization) and describes the JSON formats required.

##### Relationships and Data Flow
- Authentication Flow: Illustrate the flow from a device or user connecting → authentication (via token or mTLS) → if configured, contacting an external auth server.
- Database Interaction: Indicate how data (like user records, device sessions, logs) flows from the backend to the chosen database.
- Frontend Integration: Demonstrate that the UI (hosted as a separate build artifact) interacts with the backend via HTTP/WebSocket protocols; if deployed together, show the backend serving the static UI or proxying connections.

##### Architectural Patterns / Design Principles
- Layered Architecture: Emphasize a separation of concerns – presentation (UI), business logic (backend modules: API, broker, device, authentication), and persistence (database).
- Modular Design: Each Go package (cache, client, config, log, utils, version) represents a standalone module that can be individually developed, tested, and maintained.
- Integration with External Services: Clearly denote the role of external authentication servers and the nginx proxy setup.
- Containerization and Deployment: Include Docker and service configuration, demonstrating how the system is packaged for deployment.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Golang Backend"
        M["Golang Backend – Main Server"]:::backend
        subgraph "HTTP Layers"
            API["API (api.go)"]:::backend
            HTTP["HTTP (http.go)"]:::backend
        end
        AUTH["Authentication – Token (command.go)"]:::backend
        CONF["Configuration & mTLS (config/config.go)"]:::backend
        subgraph "Broker & Device Services"
            B["Broker Service (broker.go)"]:::backend
            D["Device Service (device.go)"]:::backend
        end
        DB["Database Integration (database.go)"]:::backend
        LOG["Logging (log/log.go)"]:::backend
        UTIL["Utilities (utils/utils.go)"]:::backend
        VER["Versioning (version/version.go)"]:::backend
        CL["Client Module (client/client.go)"]:::backend
        CACHE["Caching (cache/cache.go)"]:::backend
    end

    subgraph "Frontend"
        FE["Vue Frontend (ui/)"]:::frontend
    end

    subgraph "Deployment & External"
        DOCK["Docker Deployment (Dockerfile, build.sh)"]:::external
        NG["Nginx Proxy (rttys.conf)"]:::external
        SS["System Service (rttys.service)"]:::external
        EA["External Auth Server"]:::external
    end

    M -->|"init"| API
    M -->|"init"| HTTP
    API -->|"auth"| AUTH
    API -->|"forward"| B
    API -->|"forward"| CL
    AUTH -->|"verify"| EA
    B -->|"communicate"| D
    M -->|"config"| CONF
    M -->|"log"| LOG
    M -->|"utilize"| UTIL
    M -->|"version"| VER
    M -->|"cache"| CACHE
    M -->|"db"| DB
    FE -->|"calls"| API
    DOCK --> M
    NG --> M
    SS --> M
    API -->|"db_query"| DB
    FE -->|"ws_http"| CL

    click M "https://github.com/zhaojh329/rttys/blob/master/main.go"
    click API "https://github.com/zhaojh329/rttys/blob/master/api.go"
    click HTTP "https://github.com/zhaojh329/rttys/blob/master/http.go"
    click AUTH "https://github.com/zhaojh329/rttys/blob/master/command.go"
    click CONF "https://github.com/zhaojh329/rttys/blob/master/config/config.go"
    click B "https://github.com/zhaojh329/rttys/blob/master/broker.go"
    click D "https://github.com/zhaojh329/rttys/blob/master/device.go"
    click DB "https://github.com/zhaojh329/rttys/blob/master/database.go"
    click LOG "https://github.com/zhaojh329/rttys/blob/master/log/log.go"
    click UTIL "https://github.com/zhaojh329/rttys/blob/master/utils/utils.go"
    click VER "https://github.com/zhaojh329/rttys/blob/master/version/version.go"
    click CL "https://github.com/zhaojh329/rttys/blob/master/client/client.go"
    click CACHE "https://github.com/zhaojh329/rttys/blob/master/cache/cache.go"
    click FE "https://github.com/zhaojh329/rttys/tree/master/ui/"
    click DOCK "https://github.com/zhaojh329/rttys/tree/master/Dockerfile"
    click NG "https://github.com/zhaojh329/rttys/blob/master/rttys.conf"
    click SS "https://github.com/zhaojh329/rttys/blob/master/rttys.service"

    classDef backend fill:#e1f7d5,stroke:#333,stroke-width:2px;
    classDef frontend fill:#a2d2ff,stroke:#333,stroke-width:2px;
    classDef external fill:#ffd6a5,stroke:#333,stroke-width:2px;
```
