# giventicket/apeach-back architecture

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

- Interactive diagram: https://gitdiagram.com/giventicket/apeach-back
- Repository: https://github.com/giventicket/apeach-back

## Overview

apeach-back is a backend for voice-sample capture, custom voice-model management, and dubbing. Its API supports authentication and sample/audio processing, chunk and model operations, and AI speech tasks; the README documents sample-to-model and dubbing workflows. The sampled TTS controller shows AI-server invocation, audio storage, chunk/user updates, and webhook reporting. Scheduler and infrastructure paths are included where the excerpts or README establish their behavior; several API capabilities are represented at subsystem level because their controller implementations were not sampled.

Main entry points: [`api/routes/v2/ai`](https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/ai) (AI speech tasks), [`api/routes/v2/middlewares/decodeToken.js`](https://github.com/giventicket/apeach-back/blob/main/api/routes/v2/middlewares/decodeToken.js) (Token decoding), [`models/v2/user/user.js`](https://github.com/giventicket/apeach-back/blob/main/models/v2/user/user.js) (User records), [`api/routes/v2/models`](https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/models) (Voice model API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API workflows"]
  node_routes["Route mounting<br/>[index.js]"]
  node_auth["Auth and samples"]
  node_chunks["Chunk operations"]
  node_ai["AI speech tasks"]
  node_modelsapi["Voice model API"]
  node_usersapi["Sample lookup"]
  node_tokenmw["Token decoding<br/>[decodeToken.js]"]
end

subgraph group_domain["Voice data"]
  node_user[("User records<br/>[user.js]")]
  node_chunk[("Chunk records<br/>[chunk.js]")]
  node_model[("Voice models<br/>[model.js]")]
  node_token[("Refresh tokens<br/>[token.js]")]
end

subgraph group_infra["Runtime infrastructure"]
  node_express["Express runtime<br/>[express.js]"]
  node_mongo[("MongoDB<br/>[mongoose.js]")]
  node_storage["GCP audio storage<br/>[gcpStorage.js]"]
  node_pubsub["GCP Pub/Sub<br/>[gcpPubSubClient.js]"]
  node_subclient["Pub/Sub subscriber<br/>[gcpSubClient.js]"]
end

subgraph group_async["Background services"]
  node_sendhook["Webhook sender"]
  node_pubhook["Webhook publisher"]
  node_webhookjob["Webhook scheduler<br/>[jobSendWebhooks.js]"]
  node_bucketjob["Bucket cleanup scheduler<br/>[jobdDeleteFiles.js]"]
end

node_client(("API client"))
node_aihost["AI server"]
node_webhook["Discord webhook"]

node_client -->|"sends requests"| node_express
node_express -->|"mounts routes"| node_routes
node_routes -->|"mounts v2 routes"| node_auth
node_auth -->|"applies auth"| node_tokenmw
node_chunks -->|"applies auth"| node_tokenmw
node_ai -->|"applies auth"| node_tokenmw
node_modelsapi -->|"applies auth"| node_tokenmw
node_tokenmw -->|"queries token"| node_token
node_tokenmw -->|"loads user"| node_user
node_ai -->|"requests speech synthesis"| node_aihost
node_ai -->|"uploads generated audio"| node_storage
node_ai -->|"updates target audio"| node_chunk
node_ai -->|"updates audio count"| node_user
node_ai -->|"reports synthesis"| node_sendhook
node_auth -.->|"manages sample users"| node_user
node_chunks -.->|"manages chunks"| node_chunk
node_modelsapi -.->|"manages voice models"| node_model
node_modelsapi -->|"updates user models"| node_user
node_modelsapi -->|"updates model"| node_model
node_ai -->|"loads source chunk"| node_chunk
node_ai -->|"loads requester"| node_user
node_sendhook -->|"queues on rate limit"| node_pubhook
node_sendhook -->|"posts notifications"| node_webhook
node_pubhook -->|"publishes messages"| node_pubsub
node_webhookjob -->|"pulls queued messages"| node_subclient
node_webhookjob -->|"sends queued notices"| node_webhook
node_webhookjob -->|"requeues rate limits"| node_pubhook
node_bucketjob -->|"pulls delete jobs"| node_subclient
node_bucketjob -->|"deletes bucket files"| node_storage
node_bucketjob -->|"requeues failures"| node_pubsub
node_express -.->|"initializes database"| node_mongo

click node_express "https://github.com/giventicket/apeach-back/blob/main/loaders/express.js"
click node_routes "https://github.com/giventicket/apeach-back/blob/main/api/routes/index.js"
click node_auth "https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/auth"
click node_chunks "https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/chunks"
click node_ai "https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/ai"
click node_modelsapi "https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/models"
click node_usersapi "https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/users"
click node_tokenmw "https://github.com/giventicket/apeach-back/blob/main/api/routes/v2/middlewares/decodeToken.js"
click node_user "https://github.com/giventicket/apeach-back/blob/main/models/v2/user/user.js"
click node_chunk "https://github.com/giventicket/apeach-back/blob/main/models/v2/chunk/chunk.js"
click node_model "https://github.com/giventicket/apeach-back/blob/main/models/v2/model/model.js"
click node_token "https://github.com/giventicket/apeach-back/blob/main/models/v2/token/token.js"
click node_mongo "https://github.com/giventicket/apeach-back/blob/main/loaders/mongoose.js"
click node_storage "https://github.com/giventicket/apeach-back/blob/main/utils/gcpStorage.js"
click node_sendhook "https://github.com/giventicket/apeach-back/blob/main/utils/asyncSendWebhook.js"
click node_pubhook "https://github.com/giventicket/apeach-back/blob/main/utils/asyncPublishMessageWebhook.js"
click node_webhookjob "https://github.com/giventicket/apeach-back/blob/main/schedulers/jobSendWebhooks.js"
click node_bucketjob "https://github.com/giventicket/apeach-back/blob/main/schedulers/jobdDeleteFiles.js"
click node_pubsub "https://github.com/giventicket/apeach-back/blob/main/utils/gcpPubSubClient.js"
click node_subclient "https://github.com/giventicket/apeach-back/blob/main/utils/gcpSubClient.js"

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_routes,node_auth,node_chunks,node_ai,node_modelsapi,node_usersapi,node_tokenmw,node_client toneBlue
class node_user,node_chunk,node_model,node_token toneAmber
class node_express,node_mongo,node_storage,node_pubsub,node_subclient,node_aihost,node_webhook toneMint
class node_sendhook,node_pubhook,node_webhookjob,node_bucketjob toneRose
```

## Components

### API workflows

- [Route mounting](https://github.com/giventicket/apeach-back/blob/main/api/routes/index.js): `api/routes/index.js`
- [Auth and samples](https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/auth): `api/routes/v2/auth`
- [Chunk operations](https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/chunks): `api/routes/v2/chunks`
- [AI speech tasks](https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/ai): `api/routes/v2/ai`
- [Voice model API](https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/models): `api/routes/v2/models`
- [Sample lookup](https://github.com/giventicket/apeach-back/tree/main/api/routes/v2/users): `api/routes/v2/users`
- [Token decoding](https://github.com/giventicket/apeach-back/blob/main/api/routes/v2/middlewares/decodeToken.js): `api/routes/v2/middlewares/decodeToken.js`

### Voice data

- [User records](https://github.com/giventicket/apeach-back/blob/main/models/v2/user/user.js): `models/v2/user/user.js`
- [Chunk records](https://github.com/giventicket/apeach-back/blob/main/models/v2/chunk/chunk.js): `models/v2/chunk/chunk.js`
- [Voice models](https://github.com/giventicket/apeach-back/blob/main/models/v2/model/model.js): `models/v2/model/model.js`
- [Refresh tokens](https://github.com/giventicket/apeach-back/blob/main/models/v2/token/token.js): `models/v2/token/token.js`

### Runtime infrastructure

- [Express runtime](https://github.com/giventicket/apeach-back/blob/main/loaders/express.js): `loaders/express.js`
- [MongoDB](https://github.com/giventicket/apeach-back/blob/main/loaders/mongoose.js): `loaders/mongoose.js`
- [GCP audio storage](https://github.com/giventicket/apeach-back/blob/main/utils/gcpStorage.js): `utils/gcpStorage.js`
- [GCP Pub/Sub](https://github.com/giventicket/apeach-back/blob/main/utils/gcpPubSubClient.js): `utils/gcpPubSubClient.js`
- [Pub/Sub subscriber](https://github.com/giventicket/apeach-back/blob/main/utils/gcpSubClient.js): `utils/gcpSubClient.js`

### Background services

- [Webhook sender](https://github.com/giventicket/apeach-back/blob/main/utils/asyncSendWebhook.js): `utils/asyncSendWebhook.js`
- [Webhook publisher](https://github.com/giventicket/apeach-back/blob/main/utils/asyncPublishMessageWebhook.js): `utils/asyncPublishMessageWebhook.js`
- [Webhook scheduler](https://github.com/giventicket/apeach-back/blob/main/schedulers/jobSendWebhooks.js): `schedulers/jobSendWebhooks.js`
- [Bucket cleanup scheduler](https://github.com/giventicket/apeach-back/blob/main/schedulers/jobdDeleteFiles.js): `schedulers/jobdDeleteFiles.js`

### Other components

- **API client**
- **AI server**
- **Discord webhook**

## Connections

- API client → Express runtime: sends requests (evidence: `README.md`)
- Express runtime → Route mounting: mounts routes (evidence: `loaders/express.js`)
- Route mounting → Auth and samples: mounts v2 routes (evidence: `api/routes/index.js`)
- Auth and samples → Token decoding: applies auth (evidence: `api/routes/v2/auth/index.js`)
- Chunk operations → Token decoding: applies auth (evidence: `api/routes/v2/chunks/index.js`)
- AI speech tasks → Token decoding: applies auth (evidence: `api/routes/v2/ai/index.js`)
- Voice model API → Token decoding: applies auth (evidence: `api/routes/v2/models/index.js`)
- Token decoding → Refresh tokens: queries token (evidence: `api/routes/v2/middlewares/decodeToken.js`)
- Token decoding → User records: loads user (evidence: `api/routes/v2/middlewares/decodeToken.js`)
- AI speech tasks → AI server: requests speech synthesis (evidence: `api/routes/v2/ai/controllers/tts.js`)
- AI speech tasks → GCP audio storage: uploads generated audio (evidence: `api/routes/v2/ai/controllers/tts.js`)
- AI speech tasks → Chunk records: updates target audio (evidence: `api/routes/v2/ai/controllers/tts.js`)
- AI speech tasks → User records: updates audio count (evidence: `api/routes/v2/ai/controllers/tts.js`)
- AI speech tasks → Webhook sender: reports synthesis (evidence: `api/routes/v2/ai/controllers/tts.js`)
- Auth and samples → User records: manages sample users
- Chunk operations → Chunk records: manages chunks
- Voice model API → Voice models: manages voice models
- Voice model API → User records: updates user models (evidence: `api/routes/v2/models/controllers/updateModel.js`)
- Voice model API → Voice models: updates model (evidence: `api/routes/v2/models/controllers/updateModel.js`)
- AI speech tasks → Chunk records: loads source chunk (evidence: `api/routes/v2/ai/controllers/tts.js`)
- AI speech tasks → User records: loads requester (evidence: `api/routes/v2/ai/controllers/tts.js`)
- Webhook sender → Webhook publisher: queues on rate limit (evidence: `utils/asyncSendWebhook.js`)
- Webhook sender → Discord webhook: posts notifications (evidence: `utils/asyncSendWebhook.js`)
- Webhook publisher → GCP Pub/Sub: publishes messages (evidence: `utils/asyncPublishMessageWebhook.js`)
- Webhook scheduler → Pub/Sub subscriber: pulls queued messages (evidence: `schedulers/jobSendWebhooks.js`)
- Webhook scheduler → Discord webhook: sends queued notices (evidence: `schedulers/jobSendWebhooks.js`)
- Webhook scheduler → Webhook publisher: requeues rate limits (evidence: `schedulers/jobSendWebhooks.js`)
- Bucket cleanup scheduler → Pub/Sub subscriber: pulls delete jobs (evidence: `schedulers/jobdDeleteFiles.js`)
- Bucket cleanup scheduler → GCP audio storage: deletes bucket files (evidence: `schedulers/jobdDeleteFiles.js`)
- Bucket cleanup scheduler → GCP Pub/Sub: requeues failures (evidence: `schedulers/jobdDeleteFiles.js`)
- Express runtime → MongoDB: initializes database
