Tenancy
Identification
Pick how each HTTP request maps to a tenant before any tenant-scoped query runs.
Start with domain or subdomain for browser apps; reserve header/query/cookie
identification for trusted API clients. Pair domain-style middleware with
PreventCentral so central
hosts cannot enter tenant route groups by accident.
Middleware
| Middleware | Role |
|---|---|
pkg.ByDomain() |
Identify from full hostname |
pkg.BySubdomain() |
Identify from subdomain of a central domain |
pkg.ByDomainOrSubdomain() |
Subdomain if applicable, else full host |
pkg.ByPath() |
Identify from {tenant} path parameter |
pkg.ByRequestData() |
Identify from header, then query, then cookie |
pkg.ByOriginHeader() |
Identify from host of the Origin header |
pkg.PreventCentral() |
Block central hosts on tenant groups |
pkg.Universal() |
Identify when possible; continue centrally on miss |
pkg.EarlyIdentify() |
Early ID (prefer config flag below) |
pkg.CentralRoute() |
Mark route as central (early ID) |
pkg.TenantRoute() |
Mark route as tenant-required (early ID) |
pkg.UniversalRoute() |
Mark route as universal (early ID) |
pkg.EnsureTenant() |
Deny if tenancy was not initialized |
Helpers
| Helper | Role |
|---|---|
pkg.OnCentral(route, fn) |
Register fn on Domain for each CentralDomains host |
pkg.OnCentral(route, func(route *routing.Router) {
route.Get("/", centralHome) // one registration per CentralDomains host
})
route.Middleware(pkg.PreventCentral(), pkg.ByDomain()).Group(func(route *routing.Router) {
// tenant routes (may share method+path with central via Domain scoping)
})Runnable Herd demos for the identification middleware live under examples/
(see Examples).
Resolvers can cache successful lookups when TENANCY_RESOLVER_CACHE=true.
Invalidate with identification.InvalidateTenant after updates.
Early identification
Opt in so tenancy initializes on the Web and API stacks after the router matches a route (path parameters are available):
tenancy.New(func(cfg *tenancy.Config) {
cfg.Identification.EarlyIdentification = true
cfg.Identification.EarlyStrategy = identification.StrategyDomain // or path, subdomain, …
cfg.Identification.DefaultRouteMode = identification.ModeTenant // fail-closed
})Or set TENANCY_EARLY_IDENTIFICATION=true, TENANCY_EARLY_STRATEGY, and
TENANCY_DEFAULT_ROUTE_MODE.
Mark routes that should not follow the default mode:
route.Middleware(pkg.CentralRoute()).Get("/", centralHome)
route.Middleware(pkg.UniversalRoute()).Get("/status", status)
route.Middleware(pkg.TenantRoute()).Get("/app", tenantApp)
// unmarked routes use DefaultRouteMode (default: tenant)With early identification enabled, prefer mode markers over stacking
ByDomain / ByPath on the same routes (re-init would fail).
Universal routes
Two options:
- Per route without early ID:
route.Middleware(pkg.Universal()).Get(...). - Early ID: set
DefaultRouteModetouniversal, or mark routes withpkg.UniversalRoute().
Unknown hosts continue in the central context instead of returning 404.
Tenant-only sections can still use pkg.TenantRoute() or pkg.EnsureTenant().