# tokamak-network/trh-backend architecture

> Architecture diagram of the GitHub repository tokamak-network/trh-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tokamak-network/trh-backend
- Repository: https://github.com/tokamak-network/trh-backend

## Overview

TRH Backend is an HTTP API for administering Thanos-based chains and their integrations, with PostgreSQL-backed configuration and background operations. The principal workflow is a client request routed to an API handler, then through service logic to deployment or integration operations and persisted state. Authentication, RPC/API-key configuration, task progress, and restart recovery are supporting capabilities. Some major handlers and service modules are indexed but not excerpted, so those components are included while uncertain internal call links are omitted.

Main entry points: [`pkg/api/routes/route.go`](https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/routes/route.go) (Route Setup), [`main.go`](https://github.com/tokamak-network/trh-backend/blob/main/main.go) (Application Startup), [`pkg/api/handlers/thanos`](https://github.com/tokamak-network/trh-backend/tree/main/pkg/api/handlers/thanos) (Thanos API), [`pkg/services/thanos/service.go`](https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/service.go) (Thanos Service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_app["Application Startup<br/>[main.go]"]
  node_routes["Route Setup<br/>[route.go]"]
  node_auth["Authentication<br/>[auth.go]"]
  node_config["RPC/API Key Config<br/>[rpc_url.go]"]
  node_thanos_api["Thanos API"]
  node_deployment_api["Deployment API<br/>[deployment.go]"]
  node_integration_api["Integration API<br/>[integrations.go]"]
  node_task_api["Task Status API<br/>[handler.go]"]
end

subgraph group_domain["Thanos Operations"]
  node_thanos_service["Thanos Service<br/>[service.go]"]
  node_deployment["Deployment Operations<br/>[deployment.go]"]
  node_lifecycle["Stack Lifecycle<br/>[stack_lifecycle.go]"]
  node_integrations["Chain Integrations"]
  node_backup["Backup Management<br/>[backup_manager.go]"]
  node_uptime["Uptime Integration<br/>[uptime_service.go]"]
  node_presets["Deployment Presets<br/>[service.go]"]
  node_sdk_stack["Thanos Stack SDK<br/>[thanos_stack.go]"]
end

subgraph group_platform["Persistence and Tasks"]
  node_repositories["Postgres Repositories"]
  node_integration_repo["Integration Repository<br/>[integration.go]"]
  node_database[("PostgreSQL")]
  node_task_manager["Background Tasks<br/>[task_manager.go]"]
end

subgraph group_recovery["Recovery"]
  node_recovery_worker["Restart Recovery<br/>[recovery.go]"]
end

node_client(("API Client"))

node_app -->|"sets up"| node_routes
node_app -->|"connects"| node_database
node_app -->|"runs recovery"| node_recovery_worker
node_client -.->|"sends requests"| node_routes
node_routes -->|"routes to"| node_auth
node_routes -->|"routes to"| node_config
node_routes -->|"routes to"| node_thanos_api
node_routes -->|"routes to"| node_task_api
node_routes -->|"configures"| node_thanos_service
node_thanos_api -.->|"includes"| node_deployment_api
node_thanos_api -.->|"includes"| node_integration_api
node_thanos_service -->|"delegates"| node_deployment
node_thanos_service -->|"delegates"| node_integrations
node_deployment -->|"uses"| node_sdk_stack
node_deployment -->|"uses"| node_presets
node_backup -->|"creates clients"| node_sdk_stack
node_backup -->|"schedules tasks"| node_task_manager
node_uptime -->|"schedules tasks"| node_task_manager
node_task_api -->|"queries progress"| node_task_manager
node_integration_repo -->|"reads and writes"| node_database
node_recovery_worker -->|"uses cleanup client"| node_sdk_stack
node_recovery_worker -->|"cleans interrupted records"| node_database

click node_app "https://github.com/tokamak-network/trh-backend/blob/main/main.go"
click node_routes "https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/routes/route.go"
click node_auth "https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/auth.go"
click node_config "https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/configuration/rpc_url.go"
click node_thanos_api "https://github.com/tokamak-network/trh-backend/tree/main/pkg/api/handlers/thanos"
click node_deployment_api "https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/thanos/deployment.go"
click node_integration_api "https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/thanos/integrations.go"
click node_thanos_service "https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/service.go"
click node_deployment "https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/deployment.go"
click node_lifecycle "https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/stack_lifecycle.go"
click node_integrations "https://github.com/tokamak-network/trh-backend/tree/main/pkg/services/thanos/integrations"
click node_backup "https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/integrations/backup_manager.go"
click node_uptime "https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/integrations/uptime_service.go"
click node_presets "https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/presets/service.go"
click node_sdk_stack "https://github.com/tokamak-network/trh-backend/blob/main/pkg/stacks/thanos/thanos_stack.go"
click node_repositories "https://github.com/tokamak-network/trh-backend/tree/main/pkg/infrastructure/postgres/repositories"
click node_integration_repo "https://github.com/tokamak-network/trh-backend/blob/main/pkg/infrastructure/postgres/repositories/integration.go"
click node_task_manager "https://github.com/tokamak-network/trh-backend/blob/main/pkg/taskmanager/task_manager.go"
click node_task_api "https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/task/handler.go"
click node_recovery_worker "https://github.com/tokamak-network/trh-backend/blob/main/pkg/workers/recovery.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_app,node_routes,node_auth,node_config,node_thanos_api,node_deployment_api,node_integration_api,node_task_api,node_client toneBlue
class node_thanos_service,node_deployment,node_lifecycle,node_integrations,node_backup,node_uptime,node_presets,node_sdk_stack toneAmber
class node_repositories,node_integration_repo,node_database,node_task_manager toneMint
class node_recovery_worker toneRose
```

## Components

### HTTP API

- [Application Startup](https://github.com/tokamak-network/trh-backend/blob/main/main.go): `main.go`
- [Route Setup](https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/routes/route.go): `pkg/api/routes/route.go`
- [Authentication](https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/auth.go): `pkg/api/handlers/auth.go`
- [RPC/API Key Config](https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/configuration/rpc_url.go): `pkg/api/handlers/configuration/rpc_url.go`
- [Thanos API](https://github.com/tokamak-network/trh-backend/tree/main/pkg/api/handlers/thanos): `pkg/api/handlers/thanos`
- [Deployment API](https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/thanos/deployment.go): `pkg/api/handlers/thanos/deployment.go`
- [Integration API](https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/thanos/integrations.go): `pkg/api/handlers/thanos/integrations.go`
- [Task Status API](https://github.com/tokamak-network/trh-backend/blob/main/pkg/api/handlers/task/handler.go): `pkg/api/handlers/task/handler.go`

### Thanos Operations

- [Thanos Service](https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/service.go): `pkg/services/thanos/service.go`
- [Deployment Operations](https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/deployment.go): `pkg/services/thanos/deployment.go`
- [Stack Lifecycle](https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/stack_lifecycle.go): `pkg/services/thanos/stack_lifecycle.go`
- [Chain Integrations](https://github.com/tokamak-network/trh-backend/tree/main/pkg/services/thanos/integrations): `pkg/services/thanos/integrations`
- [Backup Management](https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/integrations/backup_manager.go): `pkg/services/thanos/integrations/backup_manager.go`
- [Uptime Integration](https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/integrations/uptime_service.go): `pkg/services/thanos/integrations/uptime_service.go`
- [Deployment Presets](https://github.com/tokamak-network/trh-backend/blob/main/pkg/services/thanos/presets/service.go): `pkg/services/thanos/presets/service.go`
- [Thanos Stack SDK](https://github.com/tokamak-network/trh-backend/blob/main/pkg/stacks/thanos/thanos_stack.go): `pkg/stacks/thanos/thanos_stack.go`

### Persistence and Tasks

- [Postgres Repositories](https://github.com/tokamak-network/trh-backend/tree/main/pkg/infrastructure/postgres/repositories): `pkg/infrastructure/postgres/repositories`
- [Integration Repository](https://github.com/tokamak-network/trh-backend/blob/main/pkg/infrastructure/postgres/repositories/integration.go): `pkg/infrastructure/postgres/repositories/integration.go`
- **PostgreSQL**
- [Background Tasks](https://github.com/tokamak-network/trh-backend/blob/main/pkg/taskmanager/task_manager.go): `pkg/taskmanager/task_manager.go`

### Recovery

- [Restart Recovery](https://github.com/tokamak-network/trh-backend/blob/main/pkg/workers/recovery.go): `pkg/workers/recovery.go`

### Other components

- **API Client**

## Connections

- Application Startup → Route Setup: sets up (evidence: `main.go`)
- Application Startup → PostgreSQL: connects (evidence: `main.go`)
- Application Startup → Restart Recovery: runs recovery (evidence: `main.go`)
- API Client → Route Setup: sends requests
- Route Setup → Authentication: routes to (evidence: `pkg/api/routes/route.go`)
- Route Setup → RPC/API Key Config: routes to (evidence: `pkg/api/routes/route.go`)
- Route Setup → Thanos API: routes to (evidence: `pkg/api/routes/route.go`)
- Route Setup → Task Status API: routes to (evidence: `pkg/api/routes/route.go`)
- Route Setup → Thanos Service: configures (evidence: `pkg/api/routes/route.go`)
- Thanos API → Deployment API: includes
- Thanos API → Integration API: includes
- Thanos Service → Deployment Operations: delegates (evidence: `pkg/services/thanos/service.go`)
- Thanos Service → Chain Integrations: delegates (evidence: `pkg/services/thanos/service.go`)
- Deployment Operations → Thanos Stack SDK: uses (evidence: `pkg/services/thanos/deployment.go`)
- Deployment Operations → Deployment Presets: uses (evidence: `pkg/services/thanos/deployment.go`)
- Backup Management → Thanos Stack SDK: creates clients (evidence: `pkg/services/thanos/integrations/backup_manager.go`)
- Backup Management → Background Tasks: schedules tasks (evidence: `pkg/services/thanos/integrations/backup_manager.go`)
- Uptime Integration → Background Tasks: schedules tasks (evidence: `pkg/services/thanos/integrations/uptime_service.go`)
- Task Status API → Background Tasks: queries progress (evidence: `pkg/api/handlers/task/handler.go`)
- Integration Repository → PostgreSQL: reads and writes (evidence: `pkg/infrastructure/postgres/repositories/integration.go`)
- Restart Recovery → Thanos Stack SDK: uses cleanup client (evidence: `pkg/workers/recovery.go`)
- Restart Recovery → PostgreSQL: cleans interrupted records (evidence: `pkg/workers/recovery.go`)
