How nbtca/saturday works
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: main.go API Router · service/event.go Event Service · model/event.go Domain Models · client.go Client Service
Components 23
Runtime
Application entrypoint and request flow
- Main · Bootstraps the server and wires dependencies
main.go
API Layer
HTTP routing and middleware boundary
- API Router · Central route assembly for the REST API
router/main.go - Event Routes · Event endpoint definitions
router/event.go - Member Routes · Member endpoint definitions
router/member.go - Client Routes · Client endpoint definitions
router/client.go - Settings Routes · Settings endpoint definitions
router/setting.go - Auth Middleware · Enforces authentication at the request boundary
middleware/auth.go - Huma Auth · Huma-specific auth and request helpers
middleware/huma_auth.go - Logger · Request logging and error translation
middleware/logger.go
Core Services
Business orchestration and domain models
- Event Service · Coordinates event business rules and side effects
service/event.go - Member Service · Coordinates member workflows
service/member.go - Client Service · Coordinates client workflows
service/client.go - Logto Service · Maps external Logto identity into internal users
service/logto.go - Webhook Service · Handles inbound webhook-driven workflows
service/webhook.go - Dify Service · Calls Dify for automation or AI-style operations
service/dify.go - Domain Models · Core persisted entities and shared types
model/event.go - DTOs · Transport-facing request and response shapes
model/dto/event.go
Persistence
Database access and schema
- DB Setup · Initializes PostgreSQL access and connection setup
repo/db.go - Event Repo · Persistence for events
repo/event.go - Member Repo · Persistence for members
repo/member.go - Client Repo · Persistence for clients
repo/client.go - Migrations · Database schema evolution scripts
migrations/000001_init.up.sql
Integrations
External systems and cross-cutting utilities
- Utilities · Shared adapters for mail, OSS, NSQ, GitHub, WeChat, auth, and validation
util/nsq.go
Connections 33
- 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