# opencsgs/csghub-server architecture

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

- Interactive diagram: https://gitdiagram.com/opencsgs/csghub-server
- Repository: https://github.com/opencsgs/csghub-server

## Overview

##### Main System Components

a) API Layer
- REST API endpoints (/api directory)
- Multiple service handlers (user, moderation, accounting, etc.)
- Authentication middleware
- Request/response handling

b) Core Components Layer
- User management
- Repository management
- Git operations
- Content moderation
- Accounting/metering
- Mirror synchronization
- Deployment management

c) Storage Layer
- Database (PostgreSQL indicated by migrations)
- S3-compatible storage (Minio)
- Git server storage
- Cache layer

d) External Services Integration
- Git servers (Gitea, potentially others)
- Content moderation services
- S3-compatible storage services
- Message queue (NATS)
- Temporal workflow engine

##### Component Relationships

- Client requests → API Gateway → Service Components
- Service Components → Storage Layer
- Async workflows through Temporal
- Event-driven processes via NATS
- Git operations flow
- File storage operations (especially LFS)

##### Specific Elements

a) Frontend Interface:
**[Client Applications] → [API Gateway]**

b) Core Services:
**[User Service]**
**[Repository Service]**
**[Model Management]**
**[Dataset Management]**
**[Content Moderation]**
**[Deployment Service]**

c) Storage Layer:
**[PostgreSQL Database]**
**[S3 Storage]**
**[Git Server]**
**[Cache Layer]**

d) External Systems:
**[Git Servers]**
**[Content Moderation Services]**
**[Message Queue]**
**[Workflow Engine]**

##### Flow Indicators

- Data flow direction
- Authentication flow
- File storage operations

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        CL[Client Applications]
    end

    subgraph "API Layer"
        AG["API Gateway"]:::api
        AM["Authentication Middleware"]:::security
        RH["Request Handlers"]:::api
    end

    subgraph "Core Services"
        direction TB
        UM["User Management"]:::core
        RM["Repository Management"]:::core
        MM["Model Management"]:::core
        DM["Dataset Management"]:::core
        SM["Space Management"]:::core
        TM["Tag Management"]:::core
        CM["Content Moderation"]:::core
        AC["Accounting/Metering"]:::core
        MS["Mirror Sync"]:::core
        DP["Deployment Management"]:::core
        EH["Event Handler"]:::core
    end

    subgraph "Storage Layer"
        DB[(PostgreSQL)]:::storage
        S3["S3 Storage (Minio)"]:::storage
        GS["Git Server Storage"]:::storage
        CH["Cache Layer"]:::storage
    end

    subgraph "External Services"
        GIT["Git Servers"]:::external
        MQ["Message Queue (NATS)"]:::external
        WF["Workflow Engine (Temporal)"]:::external
        CMS["Content Moderation Service"]:::external
    end

    %% Client to API
    CL --> AG

    %% API Layer Connections
    AG --> AM
    AM --> RH
    
    %% Core Service Connections
    RH --> UM
    RH --> RM
    RH --> MM
    RH --> DM
    RH --> SM
    RH --> TM
    RH --> CM
    RH --> AC
    RH --> MS
    RH --> DP
    RH --> EH

    %% Storage Layer Connections
    UM --> DB
    RM --> DB
    MM --> DB
    DM --> DB
    SM --> DB
    TM --> DB
    CM --> DB
    AC --> DB
    MS --> DB
    DP --> DB
    EH --> DB

    MM --> S3
    DM --> S3
    RM --> GS
    CH -.-> DB

    %% External Service Connections
    RM --> GIT
    MS --> GIT
    CM --> CMS
    EH --> MQ
    DP --> WF

    %% Click Events
    click AG "https://github.com/OpenCSGs/csghub-server/blob/main/api/router/api.go"
    click AM "https://github.com/OpenCSGs/csghub-server/blob/main/api/middleware/authenticator.go"
    click RH "https://github.com/OpenCSGs/csghub-server/tree/main/api/handler"
    click UM "https://github.com/OpenCSGs/csghub-server/tree/main/user/component"
    click RM "https://github.com/OpenCSGs/csghub-server/blob/main/component/repo.go"
    click CM "https://github.com/OpenCSGs/csghub-server/tree/main/moderation/component"
    click AC "https://github.com/OpenCSGs/csghub-server/blob/main/accounting/component/metering.go"
    click MS "https://github.com/OpenCSGs/csghub-server/tree/main/mirror"
    click DP "https://github.com/OpenCSGs/csghub-server/tree/main/builder/deploy"
    click DB "https://github.com/OpenCSGs/csghub-server/tree/main/builder/store/database"
    click S3 "https://github.com/OpenCSGs/csghub-server/blob/main/builder/store/s3/minio.go"
    click CH "https://github.com/OpenCSGs/csghub-server/tree/main/builder/store/cache"
    click GIT "https://github.com/OpenCSGs/csghub-server/tree/main/builder/git/gitserver"
    click MQ "https://github.com/OpenCSGs/csghub-server/blob/main/mq/nats.go"
    click WF "https://github.com/OpenCSGs/csghub-server/blob/main/builder/temporal/temporal.go"
    click CMS "https://github.com/OpenCSGs/csghub-server/tree/main/moderation/checker"
    click MM "https://github.com/OpenCSGs/csghub-server/blob/main/component/model.go"
    click DM "https://github.com/OpenCSGs/csghub-server/blob/main/component/dataset.go"
    click SM "https://github.com/OpenCSGs/csghub-server/blob/main/component/space.go"
    click TM "https://github.com/OpenCSGs/csghub-server/blob/main/component/tag.go"
    click EH "https://github.com/OpenCSGs/csghub-server/blob/main/component/event.go"

    classDef default fill:#fff,stroke:#333,stroke-width:2px;
    classDef api fill:#2196F3,stroke:#1565C0,color:white;
    classDef security fill:#9C27B0,stroke:#6A1B9A,color:white;
    classDef core fill:#4CAF50,stroke:#2E7D32,color:white;
    classDef storage fill:#FFC107,stroke:#FFA000,color:black;
    classDef external fill:#FF5722,stroke:#E64A19,color:white;

    %% Legend
    subgraph Legend
        L1["API Component"]:::api
        L2["Security Component"]:::security
        L3["Core Service"]:::core
        L4["Storage Component"]:::storage
        L5["External Service"]:::external
    end
```
