feat: Unleash feature flags via src/lib/flags.ts, hello-banner on GET /
Some checks failed
/ checks (push) Successful in 37s
/ deploy (push) Has been cancelled

Ported from service-template-express 9b63993.
This commit is contained in:
nholtware 2026-11-05 11:07:38 -06:00
parent 28ec495a98
commit ec4b769be2
12 changed files with 675 additions and 17 deletions

View file

@ -6,7 +6,7 @@ Express 5 + TypeScript backend, generated from `nholtware/service-template-expre
- `GET /healthz` liveness, always 200 once listening
- `GET /readyz` readiness: Postgres `SELECT 1` and Redis `PING` for whichever of `DATABASE_URL` / `REDIS_URL` are set; 503 + JSON reason otherwise
- `GET /` service name, version (git sha), pod, a `service_info` row, Redis hit counter
- `GET /` service name, version (git sha), pod, a `service_info` row, Redis hit counter, and `banner` while the `hello-banner` flag is on
- `GET /log-demo?msg=...` logs `{ demo: true, text }` with message `log-demo` (logging-pipeline demo; delete when you don't need it)
## Local dev
@ -17,7 +17,7 @@ npm ci && npm run dev
npm run lint && npm test
```
Env is validated at boot (`src/lib/config.ts`): `PORT`, `SERVICE_NAME` (required), `LOG_LEVEL`, `DATABASE_URL?`, `REDIS_URL?`.
Env is validated at boot (`src/lib/config.ts`): `PORT`, `SERVICE_NAME` (required), `LOG_LEVEL`, `DATABASE_URL?`, `REDIS_URL?`, `UNLEASH_URL?`, `UNLEASH_API_TOKEN?` (required with `UNLEASH_URL`), `UNLEASH_REFRESH_MS` (default 5000).
Migrations: SQL files in `migrations/`, applied with `npm run migrate`.
Logs: one JSON object per line on stdout (`level,time,msg,service,version`, plus `req.*`/`res.statusCode`/`responseTime` per request).
@ -35,6 +35,25 @@ Any field you pass to the logger becomes a queryable field:
overrides a `msg` key in the object, so don't name a field `msg`. Lines are searchable within a few seconds;
retention is 14 days.
## Feature flags
Flags come from Unleash (UI: http://unleash.192.168.1.83.nip.io, environment `development`).
`src/lib/flags.ts` wraps the official `unleash-client` SDK; routes get it as `deps.flags`:
```ts
if (deps.flags?.isEnabled("my-flag", { userId })) { ... }
const v = deps.flags?.getVariant("my-experiment"); // v.name, v.enabled, v.payload
```
- In the cluster the deployment sets `UNLEASH_URL=http://unleash.unleash.svc:4242/api` and
`UNLEASH_API_TOKEN` from Secret `unleash-client-token` (key `token`) in the service's namespace.
- The SDK polls every 5 s; flipping a flag in the UI changes behaviour within seconds, no redeploy.
- With `UNLEASH_URL` unset (local dev, tests) every flag reads `false` and one log line says so.
If Unleash is down, flags keep their last fetched value (or `false`) and the service keeps serving.
- Create the flag in the Unleash UI (project `default`) before using it; unknown flags read `false`.
- Tests: pass a fake `Flags` to `createApp` (see `src/app.test.ts`); never hit a real Unleash.
- `hello-banner` on `GET /` is the platform demo flag; delete it when you don't need it.
## Using this template
1. **Generate** a repo from this template in Forgejo (public). `hello-svc` is substituted into `package.json`, this README, `.env.example`, the CI workflow, and the first migration.