examples
Examples and wiring
This page shows small copy-pasteable examples demonstrating how to implement a RefreshFunc and wire the generic in-memory adapter to the HTTP server.
- An example adapter refresh function (see
internal/adapter/examples/examples_adapter.go):
- Implements
func(ctx context.Context) ([]model.Project, []model.Customer, []model.Person, error). - Returns normalized
model.Project,model.Customer, andmodel.Personslices. The generic adapter will clamp/validate fields (for exampleProgressis clamped to 0..100).
- Wire it into the example server in
cmd/exampleservice:
After creating the adapter with examples.NewExampleAdapter(), call Refresh(context.Background()) once to populate the in-memory store, then start the HTTP server. Handlers use the Adapter interface and remain agnostic of upstream details.
- Example curl calls:
List projects:
Get a project by id:
Filter by customer id (single or comma-separated):
Sort by progress:
- Run the example service (fish-compatible env setting):
Analytics and stats
Status metadata (faceting) — counts respect scoping filters but ignore explicit status/active:
Current snapshot stats (filters match /projects semantics):
Example response (abridged) from GET /stats:
Weekly rollups (when the sampler/rollup is enabled):
Notes
progress.avg/p50/p90are computed over clamped [0,100] values.durationsmay be omitted if timestamps are insufficient.- The example adapter is intentionally tiny and synchronous. Real upstream adapters typically run network calls and mapping logic inside the RefreshFunc. Prefer keeping mapping logic simple and letting the generic adapter handle cross-cutting concerns (clamping, filtering, sorting).
Startup Backfill (JetStream) — example wiring
If you enable JetStream export and startup backfill, the service will attempt to hydrate the in-memory weekly ring on boot. Example env and pseudocode:
Environment (example):
Pseudocode (example wiring in cmd/exampleservice/main.go):
Note: NewNATSBackfiller() is a placeholder that currently returns a no-op implementation; implement the JetStream pull consumer to enable real backfill.