# nbtca/saturday architecture

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

- Interactive diagram: https://gitdiagram.com/nbtca/saturday
- Repository: https://github.com/nbtca/saturday

## Overview

1. **System purpose**
   - Saturday is a Go-based REST API for the NBTCA repair service.
   - It is the backend for related client apps: the Sunday web frontend and the Hawaii WeChat mini program.

2. **Top-level runtime shape**
   - The entrypoint is `main.go`.
   - The application is organized as a layered service: **router/HTTP layer → middleware → service layer → repository layer → PostgreSQL**.
   - Cross-cutting concerns like auth, logging, and request shaping live in middleware.

3. **HTTP/API layer**
   - The `router/` package contains the endpoint definitions for core resources such as clients, events, members, and settings.
   - `router/main.go` is the central API assembly point, while files like `router/event.go` and `router/member.go` map resource-specific routes.
   - `router/input_types.go` and the DTOs under `model/dto/` show that request/response payloads are kept separate from persistence models.

4. **Middleware and request pipeline**
   - The `middleware/` package handles authentication, error translation, logging, and Huma-specific request helpers.
   - `middleware/auth.go` and `middleware/huma_auth.go` indicate that auth is enforced at the request boundary before service logic runs.
   - There is also event-action-related middleware, suggesting some requests are annotated or transformed based on business actions.

5. **Service layer as business orchestration**
   - The `service/` package contains the business workflows for clients, events, members, webhooks, Logto, and Dify.
   - Services appear to orchestrate database access plus calls to external systems, rather than exposing raw persistence directly to the router.
   - This is the main place where business rules are likely centralized.

6. **Persistence layer**
   - The `repo/` package abstracts PostgreSQL access.
   - Separate repository files for clients, events, members, roles, and DB setup indicate resource-focused data access boundaries.
   - The database schema is managed through SQL migrations under `migrations/`, with an initial schema plus incremental changes for logto IDs, GitHub fields, event size/time changes, and notification preferences.

7. **Domain and DTO split**
   - `model/` contains the core domain types for clients, events, members, settings, and notification preferences.
   - `model/dto/` contains transport-facing representations such as API request/response objects, pagination, and Logto-related DTOs.
   - This split suggests the API avoids exposing database models directly.

8. **External identity and auth**
   - The app integrates with **Logto** for user identity/authentication, based on the `service/logto.go` and related DTOs.
   - Middleware likely validates or adapts these identities into internal auth context.
   - The migrations adding `logto_id` fields confirm Logto is persisted as part of user/client identity mapping.

9. **Operational integrations**
   - The repository includes integrations for **GitHub**, **WeChat**, **mail/SMTP**, **Aliyun OSS**, **NSQ**, and **Dify** in `util/` and `service/`.
   - These are not just helpers; they materially extend the backend into notifications, webhook handling, object storage, async messaging, and AI/automation-style API calls.
   - The architecture therefore includes both synchronous API responses and side-effectful external calls.

10. **Messaging and async boundaries**
   - `util/nsq.go` plus NSQ configuration in the README indicate asynchronous event/log publishing through NSQ topics.
   - Event-related business flows likely emit messages for downstream processing or notifications instead of doing everything inline.
   - `service/webhook.go` and GitHub webhook configuration imply inbound event-driven integration as well.

11. **Configuration and deployment**
   - Configuration is environment-driven, with optional Consul-backed configuration loading when Consul variables are present.
   - The app is containerized (`Dockerfile`, `docker-compose.yaml`) and has CI/CD via GitHub Actions and CircleCI.
   - This suggests the service is designed to run as a standalone API container with external dependencies injected at runtime.

12. **Testing and fixtures**
   - Tests exist across middleware, router, and service layers, with CSV-driven fixtures under `router/testdata/`, `middleware/testdata/`, and `util/testdata/`.
   - The use of table-driven CSV fixtures implies endpoint behavior and data mapping are important validation points.
   - Documentation under `docs/` appears to describe the public API surface, reinforcing that the router layer is a stable contract boundary.

Main entry points: [`router/main.go`](https://github.com/nbtca/saturday/blob/main/router/main.go) (API Router), [`service/event.go`](https://github.com/nbtca/saturday/blob/main/service/event.go) (Event Service), [`model/event.go`](https://github.com/nbtca/saturday/blob/main/model/event.go) (Domain Models), [`service/client.go`](https://github.com/nbtca/saturday/blob/main/service/client.go) (Client Service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_runtime["Runtime"]
  node_main(("Main<br/>entrypoint<br/>[main.go]"))
end

subgraph group_grp_api["API Layer"]
  node_router_main["API Router<br/>http assembly<br/>[main.go]"]
  node_router_event["Event Routes<br/>resource routes<br/>[event.go]"]
  node_router_member["Member Routes<br/>resource routes<br/>[member.go]"]
  node_router_client["Client Routes<br/>resource routes<br/>[client.go]"]
  node_router_setting["Settings Routes<br/>resource routes<br/>[setting.go]"]
  node_middleware_auth["Auth Middleware<br/>request auth<br/>[auth.go]"]
  node_middleware_huma["Huma Auth<br/>request adapter<br/>[huma_auth.go]"]
  node_middleware_logger["Logger<br/>request logging<br/>[logger.go]"]
end

subgraph group_grp_core["Core Services"]
  node_service_event["Event Service<br/>workflow service<br/>[event.go]"]
  node_service_member["Member Service<br/>workflow service<br/>[member.go]"]
  node_service_client["Client Service<br/>workflow service<br/>[client.go]"]
  node_service_logto["Logto Service<br/>identity integration<br/>[logto.go]"]
  node_service_webhook["Webhook Service<br/>event integration<br/>[webhook.go]"]
  node_service_dify["Dify Service<br/>external api<br/>[dify.go]"]
  node_model_domain["Domain Models<br/>entities<br/>[event.go]"]
  node_model_dto["DTOs<br/>api contracts<br/>[event.go]"]
end

subgraph group_grp_persist["Persistence"]
  node_repo_db[("DB Setup<br/>postgres wiring<br/>[db.go]")]
  node_repo_event["Event Repo<br/>postgres repository<br/>[event.go]"]
  node_repo_member["Member Repo<br/>postgres repository<br/>[member.go]"]
  node_repo_client["Client Repo<br/>postgres repository<br/>[client.go]"]
  node_migrations["Migrations<br/>sql schema<br/>[000001_init.up.sql]"]
end

subgraph group_grp_integrations["Integrations"]
  node_util_integrations["Utilities<br/>integrations<br/>[nsq.go]"]
end

node_main -->|"builds API"| node_router_main
node_main -->|"opens db"| node_repo_db
node_main -->|"wires services"| node_service_event
node_router_main -->|"mounts"| node_router_event
node_router_main -->|"mounts"| node_router_member
node_router_main -->|"mounts"| node_router_client
node_router_main -->|"mounts"| node_router_setting
node_router_main -->|"uses"| node_middleware_auth
node_router_main -->|"uses"| node_middleware_huma
node_router_main -->|"uses"| node_middleware_logger
node_router_event -->|"calls"| node_service_event
node_router_member -->|"calls"| node_service_member
node_router_client -->|"calls"| node_service_client
node_router_setting -->|"uses"| node_service_client
node_service_event -->|"reads/writes"| node_repo_event
node_service_member -->|"reads/writes"| node_repo_member
node_service_client -->|"reads/writes"| node_repo_client
node_service_event -.->|"auth links"| node_service_logto
node_service_event -.->|"emits/handles"| node_service_webhook
node_service_event -.->|"external calls"| node_service_dify
node_service_event -.->|"uses"| node_util_integrations
node_service_member -.->|"uses"| node_util_integrations
node_service_client -.->|"uses"| node_util_integrations
node_repo_event -->|"maps"| node_model_domain
node_repo_member -->|"maps"| node_model_domain
node_repo_client -->|"maps"| node_model_domain
node_router_event -->|"serializes"| node_model_dto
node_router_member -->|"serializes"| node_model_dto
node_router_client -->|"serializes"| node_model_dto
node_service_event -->|"orchestrates"| node_model_domain
node_service_member -->|"orchestrates"| node_model_domain
node_service_client -->|"orchestrates"| node_model_domain
node_repo_db -.->|"schema"| node_migrations

click node_main "https://github.com/nbtca/saturday/blob/main/main.go"
click node_router_main "https://github.com/nbtca/saturday/blob/main/router/main.go"
click node_router_event "https://github.com/nbtca/saturday/blob/main/router/event.go"
click node_router_member "https://github.com/nbtca/saturday/blob/main/router/member.go"
click node_router_client "https://github.com/nbtca/saturday/blob/main/router/client.go"
click node_router_setting "https://github.com/nbtca/saturday/blob/main/router/setting.go"
click node_middleware_auth "https://github.com/nbtca/saturday/blob/main/middleware/auth.go"
click node_middleware_huma "https://github.com/nbtca/saturday/blob/main/middleware/huma_auth.go"
click node_middleware_logger "https://github.com/nbtca/saturday/blob/main/middleware/logger.go"
click node_service_event "https://github.com/nbtca/saturday/blob/main/service/event.go"
click node_service_member "https://github.com/nbtca/saturday/blob/main/service/member.go"
click node_service_client "https://github.com/nbtca/saturday/blob/main/service/client.go"
click node_service_logto "https://github.com/nbtca/saturday/blob/main/service/logto.go"
click node_service_webhook "https://github.com/nbtca/saturday/blob/main/service/webhook.go"
click node_service_dify "https://github.com/nbtca/saturday/blob/main/service/dify.go"
click node_repo_db "https://github.com/nbtca/saturday/blob/main/repo/db.go"
click node_repo_event "https://github.com/nbtca/saturday/blob/main/repo/event.go"
click node_repo_member "https://github.com/nbtca/saturday/blob/main/repo/member.go"
click node_repo_client "https://github.com/nbtca/saturday/blob/main/repo/client.go"
click node_model_domain "https://github.com/nbtca/saturday/blob/main/model/event.go"
click node_model_dto "https://github.com/nbtca/saturday/blob/main/model/dto/event.go"
click node_util_integrations "https://github.com/nbtca/saturday/blob/main/util/nsq.go"
click node_migrations "https://github.com/nbtca/saturday/blob/main/migrations/000001_init.up.sql"

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_main toneBlue
class node_router_main,node_router_event,node_router_member,node_router_client,node_router_setting,node_middleware_auth,node_middleware_huma,node_middleware_logger toneAmber
class node_service_event,node_service_member,node_service_client,node_service_logto,node_service_webhook,node_service_dify,node_model_domain,node_model_dto toneMint
class node_repo_db,node_repo_event,node_repo_member,node_repo_client,node_migrations toneRose
class node_util_integrations toneIndigo
```

## Components

### Runtime

Application entrypoint and request flow

- [Main](https://github.com/nbtca/saturday/blob/main/main.go): entrypoint · Bootstraps the server and wires dependencies · `main.go`

### API Layer

HTTP routing and middleware boundary

- [API Router](https://github.com/nbtca/saturday/blob/main/router/main.go): http assembly · Central route assembly for the REST API · `router/main.go`
- [Event Routes](https://github.com/nbtca/saturday/blob/main/router/event.go): resource routes · Event endpoint definitions · `router/event.go`
- [Member Routes](https://github.com/nbtca/saturday/blob/main/router/member.go): resource routes · Member endpoint definitions · `router/member.go`
- [Client Routes](https://github.com/nbtca/saturday/blob/main/router/client.go): resource routes · Client endpoint definitions · `router/client.go`
- [Settings Routes](https://github.com/nbtca/saturday/blob/main/router/setting.go): resource routes · Settings endpoint definitions · `router/setting.go`
- [Auth Middleware](https://github.com/nbtca/saturday/blob/main/middleware/auth.go): request auth · Enforces authentication at the request boundary · `middleware/auth.go`
- [Huma Auth](https://github.com/nbtca/saturday/blob/main/middleware/huma_auth.go): request adapter · Huma-specific auth and request helpers · `middleware/huma_auth.go`
- [Logger](https://github.com/nbtca/saturday/blob/main/middleware/logger.go): request logging · Request logging and error translation · `middleware/logger.go`

### Core Services

Business orchestration and domain models

- [Event Service](https://github.com/nbtca/saturday/blob/main/service/event.go): workflow service · Coordinates event business rules and side effects · `service/event.go`
- [Member Service](https://github.com/nbtca/saturday/blob/main/service/member.go): workflow service · Coordinates member workflows · `service/member.go`
- [Client Service](https://github.com/nbtca/saturday/blob/main/service/client.go): workflow service · Coordinates client workflows · `service/client.go`
- [Logto Service](https://github.com/nbtca/saturday/blob/main/service/logto.go): identity integration · Maps external Logto identity into internal users · `service/logto.go`
- [Webhook Service](https://github.com/nbtca/saturday/blob/main/service/webhook.go): event integration · Handles inbound webhook-driven workflows · `service/webhook.go`
- [Dify Service](https://github.com/nbtca/saturday/blob/main/service/dify.go): external api · Calls Dify for automation or AI-style operations · `service/dify.go`
- [Domain Models](https://github.com/nbtca/saturday/blob/main/model/event.go): entities · Core persisted entities and shared types · `model/event.go`
- [DTOs](https://github.com/nbtca/saturday/blob/main/model/dto/event.go): api contracts · Transport-facing request and response shapes · `model/dto/event.go`

### Persistence

Database access and schema

- [DB Setup](https://github.com/nbtca/saturday/blob/main/repo/db.go): postgres wiring · Initializes PostgreSQL access and connection setup · `repo/db.go`
- [Event Repo](https://github.com/nbtca/saturday/blob/main/repo/event.go): postgres repository · Persistence for events · `repo/event.go`
- [Member Repo](https://github.com/nbtca/saturday/blob/main/repo/member.go): postgres repository · Persistence for members · `repo/member.go`
- [Client Repo](https://github.com/nbtca/saturday/blob/main/repo/client.go): postgres repository · Persistence for clients · `repo/client.go`
- [Migrations](https://github.com/nbtca/saturday/blob/main/migrations/000001_init.up.sql): sql schema · Database schema evolution scripts · `migrations/000001_init.up.sql`

### Integrations

External systems and cross-cutting utilities

- [Utilities](https://github.com/nbtca/saturday/blob/main/util/nsq.go): integrations · Shared adapters for mail, OSS, NSQ, GitHub, WeChat, auth, and validation · `util/nsq.go`

## Connections

- Main → API Router: builds API · Main assembles the HTTP surface
- Main → DB Setup: opens db · Main initializes persistence connectivity
- Main → Event Service: wires services · Application startup connects business services
- API Router → Event Routes: mounts · Central router registers event routes
- API Router → Member Routes: mounts · Central router registers member routes
- API Router → Client Routes: mounts · Central router registers client routes
- API Router → Settings Routes: mounts · Central router registers settings routes
- API Router → Auth Middleware: uses · Routes are protected by auth middleware
- API Router → Huma Auth: uses · Routes use Huma request/auth adapters
- API Router → Logger: uses · Routes pass through logging/error middleware
- Event Routes → Event Service: calls · Event endpoints delegate to event workflows
- Member Routes → Member Service: calls · Member endpoints delegate to member workflows
- Client Routes → Client Service: calls · Client endpoints delegate to client workflows
- Settings Routes → Client Service: uses · Settings routes depend on application services
- Event Service → Event Repo: reads/writes · Event workflows persist through the event repository
- Member Service → Member Repo: reads/writes · Member workflows persist through the member repository
- Client Service → Client Repo: reads/writes · Client workflows persist through the client repository
- Event Service → Logto Service: auth links · Event workflows may resolve external identities
- Event Service → Webhook Service: emits/handles · Event workflows interact with webhook-driven flows
- Event Service → Dify Service: external calls · Event workflows can call Dify integrations
- Event Service → Utilities: uses · Event workflows depend on shared utility adapters
- Member Service → Utilities: uses · Member workflows depend on shared utility adapters
- Client Service → Utilities: uses · Client workflows depend on shared utility adapters
- Event Repo → Domain Models: maps · Repositories map database rows to domain models
- Member Repo → Domain Models: maps · Repositories map database rows to domain models
- Client Repo → Domain Models: maps · Repositories map database rows to domain models
- Event Routes → DTOs: serializes · Routes use DTOs for request and response payloads
- Member Routes → DTOs: serializes · Routes use DTOs for request and response payloads
- Client Routes → DTOs: serializes · Routes use DTOs for request and response payloads
- Event Service → Domain Models: orchestrates · Business logic operates on core domain types
- Member Service → Domain Models: orchestrates · Business logic operates on core domain types
- Client Service → Domain Models: orchestrates · Business logic operates on core domain types
- DB Setup → Migrations: schema · Database setup relies on the migration history
