# fudengming/astrbot-t2irouter architecture

> Architecture diagram of the GitHub repository fudengming/astrbot-t2irouter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fudengming/astrbot-t2irouter
- Repository: https://github.com/fudengming/astrbot-t2irouter

## Overview

AstrBot T2I Router is a Go HTTP router that selects the first reachable configured text-to-image backend and keeps generated-image lookups pinned to that backend. The principal flow is generation request → backend selection → forwarding or redirect → ID binding → later data lookup and redirect. In-memory bindings expire by TTL and are also cleaned in batches. The source excerpt is partial, so some relationships below rely on the README’s explicit workflow description rather than inspected implementation details.

Main entry points: [`main.go`](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go) (Generate Handler)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Router Runtime"]
  node_config_server["Config and Server<br/>[main.go]"]
  node_http_dispatch["HTTP Dispatch<br/>[main.go]"]
end

subgraph group_generation["Generation Routing"]
  node_generate["Generate Handler<br/>[main.go]"]
  node_backend_select["Backend Selection<br/>[main.go]"]
  node_connectivity_check["TCP Connectivity Check<br/>[main.go]"]
  node_forward["Request Forwarding<br/>[main.go]"]
  node_response_id["Response ID Parsing<br/>[main.go]"]
end

subgraph group_bindings["ID Bindings"]
  node_binding_store[("Binding Table<br/>[main.go]")]
  node_cleanup["Binding Cleanup<br/>[main.go]"]
  node_data_handler["Data Handler<br/>[main.go]"]
  node_redirect["307 Redirect<br/>[main.go]"]
end

subgraph group_backends["Backend Services"]
  node_backend["Text-to-Image Backend"]
end

node_client(("Client"))

node_client -->|"sends requests"| node_http_dispatch
node_config_server -->|"serves requests"| node_http_dispatch
node_http_dispatch -->|"routes generation"| node_generate
node_http_dispatch -->|"routes data lookup"| node_data_handler
node_generate -->|"selects backend"| node_backend_select
node_backend_select -->|"checks reachability"| node_connectivity_check
node_connectivity_check -->|"tests TCP connection"| node_backend
node_generate -->|"forwards JSON request"| node_forward
node_forward -->|"sends generation request"| node_backend
node_generate -->|"redirects non-JSON request"| node_redirect
node_backend -->|"returns response"| node_response_id
node_generate -->|"parses response ID"| node_response_id
node_generate -->|"binds ID to backend"| node_binding_store
node_cleanup -->|"deletes expired entries"| node_binding_store
node_data_handler -->|"looks up ID"| node_binding_store
node_data_handler -->|"redirects to bound backend"| node_redirect
node_redirect -->|"returns 307"| node_client

click node_config_server "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_http_dispatch "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_generate "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_backend_select "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_connectivity_check "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_forward "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_response_id "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_binding_store "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_cleanup "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_data_handler "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"
click node_redirect "https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_config_server,node_http_dispatch,node_client toneBlue
class node_generate,node_backend_select,node_connectivity_check,node_forward,node_response_id toneAmber
class node_binding_store,node_cleanup,node_data_handler,node_redirect toneMint
class node_backend toneRose
```

## Components

### Router Runtime

- [Config and Server](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`
- [HTTP Dispatch](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`

### Generation Routing

- [Generate Handler](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`
- [Backend Selection](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`
- [TCP Connectivity Check](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`
- [Request Forwarding](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`
- [Response ID Parsing](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`

### ID Bindings

- [Binding Table](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`
- [Binding Cleanup](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`
- [Data Handler](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`
- [307 Redirect](https://github.com/fudengming/astrbot-t2irouter/blob/main/main.go): `main.go`

### Backend Services

- **Text-to-Image Backend**

### Other components

- **Client**

## Connections

- Client → HTTP Dispatch: sends requests (evidence: `README.md`)
- Config and Server → HTTP Dispatch: serves requests (evidence: `main.go`)
- HTTP Dispatch → Generate Handler: routes generation (evidence: `main.go`)
- HTTP Dispatch → Data Handler: routes data lookup (evidence: `main.go`)
- Generate Handler → Backend Selection: selects backend (evidence: `README.md`)
- Backend Selection → TCP Connectivity Check: checks reachability (evidence: `README.md`)
- TCP Connectivity Check → Text-to-Image Backend: tests TCP connection (evidence: `README.md`)
- Generate Handler → Request Forwarding: forwards JSON request (evidence: `README.md`)
- Request Forwarding → Text-to-Image Backend: sends generation request (evidence: `README.md`)
- Generate Handler → 307 Redirect: redirects non-JSON request (evidence: `README.md`)
- Text-to-Image Backend → Response ID Parsing: returns response (evidence: `README.md`)
- Generate Handler → Response ID Parsing: parses response ID (evidence: `main.go`)
- Generate Handler → Binding Table: binds ID to backend (evidence: `main.go`)
- Binding Cleanup → Binding Table: deletes expired entries (evidence: `main.go`)
- Data Handler → Binding Table: looks up ID (evidence: `main.go`)
- Data Handler → 307 Redirect: redirects to bound backend (evidence: `README.md`)
- 307 Redirect → Client: returns 307 (evidence: `README.md`)
