Skip to Content
gentlemans-stack

Architecture

The layers of the Nx monorepo, the platforms folder, the IAM service, observability and the generator flags that switch them on.

The repo is an Nx monorepo, layered bottom-up. AGENTS.md keeps the rules an agent must follow; this page holds the detail behind them. The reasons for each choice go in stack.md and principles.md.

Layers

The order, from the bottom up, is models/ (portable schemas), then libs/ (foundation and contract machinery) and connectors/ (isolated infrastructure adapters), then contracts/ (public service APIs) and domains/ (Mongoose repositories), then services/ and apps/ (composition roots).

Four layers, from services and apps at the top to models at the bottom, each importing only from the layers below

ui-libs/design-tokens (tokens.json) feeds both the web ui-libs/design-system and every Flutter app under apps/. Its outputs are generated and never hand-edited; adding-a-mobile-app.md lists them.

models/todo-model, contracts/todo-contracts, domains/todo-domain, services/todo-service and apps/todo-ui form the live worked example. Point a generator at it and diff against it. Do not hand-copy a real product's code. A request through it passes the rate limit, authentication and the handler, and keeps one trace id.

A request from todo-ui through the rate limit, IAM authentication and the handler to Redis and MongoDB

Platforms

platforms/<cloud>/<pkg> holds code that only makes sense on one cloud. Today there are two packages.

  • platforms/gcp/auth-firebase holds the Firebase AuthProviders built on libs/auth.
  • platforms/gcp/cloud-run holds the Cloud Run deploy and cleanup scripts. They are bash, not an Nx project, and build on the core docker:build target.

A platform package may import libs/, connectors/ and models/. Only services/ and apps/ import a platform, and the core never does. The fallow platforms zone enforces this with boundary-violation: error, checked by fallow architecture --boundaries. Keep each vendor SDK behind its own subpath export so a service that does not use it never loads it.

IAM service

services/iam-service is the identity service. It also serves contracts/workspace-contracts (workspaces, members and invitations), with its data in domains/workspace-domain.

Other services ask IAM for the session; only IAM reads the auth collections in MongoDB

How a browser or a service call becomes a session: todo-service asks iam-service with the cookie cache off and caches the answer for the TTL

  • Better Auth is mounted at /api/auth through the mounts seam of libs/server-setup, which also merges Better Auth's OpenAPI into /docs.
  • IAM owns the user, session, account and verification collections. Every service shares one gentlemans-stack MongoDB database and one MONGO_URI, so a domain owns collections, not a database.
  • Local Mongo is a single-node replica set because Better Auth uses transactions.
  • pnpm check:iam-boundary fails when code outside services/iam-service reads Better Auth's collections.

The details are in the IAM service README.

Observability

libs/observability is the OpenTelemetry layer, covering traces, metrics and logs over OTLP to the local Collector and Grafana in LOCAL_INFRA. Its README lists the subpaths and env vars.

The browser and the services send telemetry over OTLP to the Collector, which forwards it to Grafana

  • A service loads it as a second esbuild entry, src/instrument.ts, with node --require ./instrument.js main.js.
  • apps/todo-ui starts /web from the root route.
  • Every API is a no-op when OTEL_SDK_DISABLED=true, which tests set.

Generator flags

The generators in dependency order: model, then contract and domain, then the service and the web app, with the mobile app standalone

deployable-service also takes --auth=dev|iam. The default is dev, the placeholder createDevAuthProvider. With iam, callers resolve through IAM's session endpoint with createIamSessionAuthProvider, and the generator adds <PREFIX>_IAM_URL, _IAM_TIMEOUT_MS and _IAM_SESSION_CACHE_TTL_MS.

deployable-service and deployable-web-app take --observability=otel|none. The default is otel, which adds the dependency, src/instrument.ts, the build entry and serve flag, the Dockerfile --require, the log drain and the span middleware. For the app it also adds the root-route start-up and VITE_OTEL_COLLECTOR_URL.

deployable-service also takes two cloud flags. Both default to none, so the core stays cloud-free.

  • --platform=gcp|none adds @gentlemans-stack/auth-firebase as a dependency and a tsconfig reference.
  • --deploy=cloud-run|none needs --platform=gcp. It writes services/<x>/cloud-run.config.sh and a deploy target that runs platforms/gcp/cloud-run/deploy-cloud-run.sh.

There is no generator for a platforms/<cloud>/<pkg> package yet. Hand-write it like platforms/gcp/auth-firebase.

On this page