PrimeBrickPrimeBrick
  • Docs
  • Contact
  • MIT License
  • Documentation
  • MCP Server
  • API Catalog
  • Services
  • Libraries
PrimeBrickPrimeBrick

© 2026 PrimeBrick. MIT License. v3.11.2

github
DAL Library
SDK Library
    Developer Guide
      OverviewGetting StartedAnatomy of a MicroserviceAuthenticationClient Shield & Rotating SecretsHTTP ServerRPC RouterEntity RouterOpenAPI GenerationDocs PackConfig tables & ConfigLoaderDatabase MigrationsTranslationsCurrency UtilitiesNATS™ ClientService RegistrationSSE StandardPresenceExt-JSONRedis cache layerTelemetry & LoggingAPI Reference
Decimal Library
powered by Zudoku
Developer Guide

Anatomy of a Microservice

microservice/create-microservice.ts is the facade every Primebrick service boots through. It sequences ~12 wired stages — env, DAL, config, auth, NATS, shield, HTTP, registration, health, shutdown — so a service is declared by its options, not by handwritten infrastructure.

Boot sequence

Code
installConsoleBridge → every log line gets ts + trace_id requireEnv(envSchema) → loud preflight; missing .env/vars = fatal, stderr + throw initDal() + dbBanner() → consumer's own DB init (SDK stays DB-agnostic) ConfigLoader.load() → <schema>.config_entries via ConfigRepositoryPort initAuthConfig + loadAuthConfig → auth resolved from config (ports, not env) apiKeyPort? / healthCheckPort / authDependencySetters NatsClient.getConnection(nats_url) → joins the fabric client-registry + secret provider → identity + shield slot warm setNatsShieldRegistry + shields → inbound verification armed HTTP server (routeHandler) → client-identity gate → auth → RBAC → validate → handler ServiceRegistrar.register + heartbeat → announces to the BE registry docs-pack manifest check → capabilities += "docs_pack" onReady(ctx) → service-specific wiring (subscribers, jobs) graceful-shutdown hooks → reverse-order teardown (dalClose, nats drain, http close)

The MicroserviceContext returned exposes env, configLoader, natsClient, registrar, healthCheck — the handles onReady uses to wire the service's own logic.

Topology — where a service sits

A microservice never answers public HTTP. Its HTTP surface exists only to be called by the BE proxy (or by other services over internal fetch); its outbound face is the NATS fabric.

Two authentication layers — L1 and L2

Every inbound call clears two distinct walls:

LayerQuestionMechanismDetails
L1 — client/networkIs the caller a trusted client?x-client-shield-secret (rotating) + client-identity gate (User-Agent vs client-registry)client-shield-and-secrets
L2 — user/RBACMay this user do this?resolved AuthUser → permissions[] on the RpcRoute, defaultDenyauthentication, rpc-router

The same split applies on NATS: verifyNatsPublisher + subscribeShielded (L1) before any subject handler runs.

The NATS fabric

Subject familyDirectionPurpose
service.register / service.unregister / service.heartbeatUS → BElifecycle announcements the registry consumes
service.gateway_onlineBE → all"the gateway is back" — triggers re-registration and re-pulls
config.getUS → BEgoverned config lookups
secret.getUS → BEcorrelation-route request for the shield slot (allowlisted)
secret.rotatedBE → allshield rotation broadcast — consumers refresh their slot
client_shield_registry.getUS → BEknown-client lookups for the identity gate
webhook.<code>.receivedwebhook US → target USJetStream stream for durable webhook delivery

Pub/sub is the default; JetStream only where durability/replay matters. NATS request/reply is deprecated — governed correlation routes (makeNatsRequestRoute) replaced it.

What the service must declare

Required: serviceName, envSchema, initDal, dalClose, configRepositoryAdapter, authConfigPort, healthCheckPort. Optional: routeHandler (factory-produced only — makeRpcRouter, makeOpenApiHandler, composeRouteHandlers; arbitrary handlers are rejected), apiKeyPort, endpoints, customHealthChecks, identityExemptPaths, docsPackDir, authDependencySetters, onReady, identity/display/icon fields for the registry.

Deep-dives

  • Boot options & context → this page + service-registration.mdx
  • HTTP routes & middleware chain → rpc-router.mdx, http-server.mdx
  • Entity CRUD factory → entity-router.mdx
  • Spec from routes → openapi-generation.mdx
  • Docs to the KB → docs-pack.mdx
  • Secrets & shield → client-shield-and-secrets.mdx
  • NATS client → nats-client.mdx; SSE → sse-standard.mdx
Last modified on October 28, 2026
Getting StartedAuthentication
On this page
  • Boot sequence
  • Topology — where a service sits
  • Two authentication layers — L1 and L2
  • The NATS fabric
  • What the service must declare
  • Deep-dives