|
| 1 | +# Callora Backend AI Guidance |
| 2 | + |
| 3 | +- The repo is a Node.js + TypeScript backend for an API marketplace, gateway, usage metering, billing, and Stellar/Soroban settlement. |
| 4 | +- Primary runtime entrypoint: `src/index.ts`. The file wires middleware, route groups, and background jobs, then starts Express. |
| 5 | +- The app exports `app` and `default app` for tests; runtime startup is gated behind a direct-execution check so Jest can import without starting the server. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +- `src/routes/*` contains HTTP route groups: |
| 10 | + - `/api/developers` via `src/routes/developerRoutes.ts` |
| 11 | + - `/api/admin` via `src/routes/admin/*.ts` |
| 12 | + - `/api/refunds` via `src/routes/refunds.ts` |
| 13 | + - `/api/gateway` for legacy gateway traffic |
| 14 | + - `/v1/call` for the newer proxy path |
| 15 | +- `src/services/*` holds business logic, billing, rate limiting, revenue settlement, and scheduled worker jobs. |
| 16 | +- `src/repositories/*` implements persistence abstractions. Most logic should call repository methods, not raw SQL. |
| 17 | +- DB wiring is split: |
| 18 | + - `src/db.ts` is the primary Postgres pool and replica-aware `readQuery`/`writeQuery` helpers. |
| 19 | + - `src/db/index.ts` is a local SQLite/drizzle helper used for lightweight migrations/tests. |
| 20 | +- Configuration is validated in `src/config/env.ts` using Zod and exposed via `src/config/index.ts`. |
| 21 | + |
| 22 | +## Important runtime patterns |
| 23 | + |
| 24 | +- Middleware order matters: request IDs, SLO recorder, route body limit middleware, then JSON parsing. `/api/webhooks` intentionally skips `express.json()`. |
| 25 | +- `src/index.ts` starts caches and workers before listening: |
| 26 | + - `listingsCache` warmup |
| 27 | + - `refundsCache` warmup |
| 28 | + - `RevenueLedgerIndexer`, `SettlementStatusSync`, `IdempotencySweeper`, `SettlementRecon`, `SlowQueryAlerter`, `AnomalyDetector`, `MonthlyInvoiceJob`, `SloAlertJob` |
| 29 | +- Graceful shutdown is implemented with `createGracefulShutdownHandler` and `createInFlightDrainTracker` for proxy traffic. |
| 30 | +- Rate limiter store mode is environment-driven: `RATE_LIMIT_STORE=memory` or `postgres`. |
| 31 | + |
| 32 | +## Developer workflows |
| 33 | + |
| 34 | +- Install dependencies: `npm install` |
| 35 | +- Local dev server: `npm run dev` (`tsx watch src/index.ts`) |
| 36 | +- Build: `npm run build` |
| 37 | +- Run tests: `npm test` |
| 38 | +- Unit-only: `npm run test:unit` |
| 39 | +- Integration-only: `npm run test:integration` |
| 40 | +- Coverage: `npm run test:coverage` |
| 41 | +- Migrations: `npm run db:migrate` |
| 42 | +- Drizzle Studio: `npm run db:studio` |
| 43 | + |
| 44 | +## Project-specific conventions |
| 45 | + |
| 46 | +- `npm run prebuild` and `npm run pretest` invoke `npm run error-codes:check`. |
| 47 | +- Error code changes should be made in `docs/error-codes.yaml` and then synced with `src/errors/codes.ts` via `npm run error-codes:generate`. |
| 48 | +- `src/routes/*` use factory functions like `createDeveloperRouter(...)` to receive explicit dependencies instead of global imports. |
| 49 | +- Always preserve the health and metrics endpoints in `src/index.ts` before other route registrations. |
| 50 | + |
| 51 | +## Integration points and external dependencies |
| 52 | + |
| 53 | +- PostgreSQL is the main persistent store, configured by `DATABASE_URL` and optional `REPLICA_URLS`. |
| 54 | +- Stellar/Soroban integration is configured through `STELLAR_*` and `SOROBAN_*` env vars; runtime logic uses `config.stellar` and `config.sorobanRpc`. |
| 55 | +- Proxy upstream target is configured by `UPSTREAM_URL` and host allowlist validation in `src/config/index.ts`. |
| 56 | +- Key environment variables: |
| 57 | + - `JWT_SECRET` |
| 58 | + - `ADMIN_API_KEY` |
| 59 | + - `METRICS_API_KEY` |
| 60 | + - `DATABASE_URL` |
| 61 | + - `RATE_LIMIT_STORE` |
| 62 | + |
| 63 | +## Key files to inspect first |
| 64 | + |
| 65 | +- `src/index.ts` |
| 66 | +- `src/config/env.ts` |
| 67 | +- `src/config/index.ts` |
| 68 | +- `src/routes` |
| 69 | +- `src/services` |
| 70 | +- `src/repositories` |
| 71 | +- `src/db.ts` |
| 72 | +- `src/db/index.ts` |
| 73 | +- `Dockerfile` |
| 74 | +- `docker-compose.yml` |
| 75 | +- `README.md` |
| 76 | +- `RESILIENCE.md` |
| 77 | + |
| 78 | +> If any section is unclear or missing important runtime details, please point it out and I will refine the guide. |
0 commit comments