Documentation
¶
Overview ¶
Package server wires the HTTP routes and middleware.
Index ¶
- Constants
- func Correlation() gin.HandlerFunc
- func CorrelationID(c *gin.Context) string
- func Middleware(accessLog *slog.Logger, requestTimeout time.Duration) []gin.HandlerFunc
- func ProtectedGroup(r *gin.Engine, client *authz.Client, creds *domain.Credentials, ...) *gin.RouterGroup
- func RateLimit(pool *redis.Pool, opts ratelimit.Options, creds *domain.Credentials) []gin.HandlerFunc
- func RedactTicket() gin.HandlerFunc
- func RequestDeadline(d time.Duration) gin.HandlerFunc
- func SetupHandlers(r *gin.Engine, s Services)
- type Check
- type EntitlementItem
- type ErrorBody
- type Readiness
- type Services
- type StreamOptions
Constants ¶
const ( CloseCredentialRevoked websocket.StatusCode = 4001 // the credential that opened the stream is gone CloseMembershipChanged websocket.StatusCode = 4002 // access changed or ended; reconnect to find out CloseSlowConsumer websocket.StatusCode = 4003 // the client fell behind; reconnect and refetch CloseMaxAge websocket.StatusCode = 4004 // the connection reached its maximum age )
WebSocket close codes the service sends (application range 4000-4999).
Variables ¶
This section is empty.
Functions ¶
func Correlation ¶
func Correlation() gin.HandlerFunc
Correlation reuses the caller's X-Correlation-ID or mints one, and echoes it on the response so a request can be followed across services and log lines.
func CorrelationID ¶
CorrelationID returns the request's correlation ID, or "" outside the Correlation middleware.
func Middleware ¶ added in v0.10.0
Middleware is the chain every request runs first, in the order that matters: correlation, then ticket redaction (before anything that logs the URL), then the access log, recovery and tracing. main and the tests build the router from this one list, so the order cannot drift apart.
func ProtectedGroup ¶
func ProtectedGroup(r *gin.Engine, client *authz.Client, creds *domain.Credentials, roles ...string) *gin.RouterGroup
ProtectedGroup returns a route group whose every route requires a bearer token that auth-api verifies (signature, expiry, iss, aud) for this service and whose user holds one of roles (default: RoleUser). Missing or invalid tokens get 401, a missing role gets 403. Routes added to the group inherit this; handlers never repeat the check. The caller's own token is the only credential, per PADR-0037 (no service tokens), and a client application identity alone is a 401.
func RateLimit ¶ added in v0.2.0
func RateLimit(pool *redis.Pool, opts ratelimit.Options, creds *domain.Credentials) []gin.HandlerFunc
RateLimit returns the per-client limiter (PADR-0036) preceded by a step that sets its client key. The shared limiter keys on X-API-Key, else the IP, and both are caller-controlled, so that step never lets the caller's header through:
- a bearer that resolves to a live integration credential is keyed by the credential's ID, so credentials are distinct clients;
- everything else, including a made-up "ic_" secret, is keyed by IP.
func RedactTicket ¶ added in v0.10.0
func RedactTicket() gin.HandlerFunc
RedactTicket takes the stream ticket out of the request URL before anything logs it, and keeps it for the stream handler. The ticket is single-use and short-lived, so a leaked one is mostly harmless, but it should never reach an access log.
func RequestDeadline ¶ added in v0.11.0
func RequestDeadline(d time.Duration) gin.HandlerFunc
RequestDeadline bounds every request but the event stream's upgrade (a long-lived connection by design) to d, so a slow dependency cannot hold a request, and the goroutine behind it, open. A store call that runs past it returns context.DeadlineExceeded, which the error mapper reports as 503 timeout.
func SetupHandlers ¶
SetupHandlers registers every route group on r.
Types ¶
type EntitlementItem ¶
type EntitlementItem struct {
Key string `json:"key"`
Type string `json:"type"`
Value any `json:"value"`
Usage *int64 `json:"usage,omitempty"`
}
EntitlementItem is one key as the caller holds it. Value is a bool for flags and an integer for limits. Usage is the caller's count against a limit; it is omitted for flags and for limits whose resource this service does not store yet.
type ErrorBody ¶
type ErrorBody struct {
Code string `json:"code"`
Message string `json:"message"`
CorrelationID string `json:"correlation_id,omitempty"`
Key string `json:"key,omitempty"`
Limit *int `json:"limit,omitempty"`
Current any `json:"current,omitempty"`
}
ErrorBody is the typed error response: a stable code, a safe message, the correlation ID, and for entitlement denials the key and limit. Current carries the stored record on a 409.
type Readiness ¶ added in v0.11.0
type Readiness struct {
// contains filtered or unexported fields
}
Readiness is the deep dependency check behind /status/health. PADR-0017 keeps it off the readiness and liveness probes (both use /status/ping): it is for operators and dashboards and must not gate routing or restarts. Each check runs under a timeout, and the answer is cached for ttl, so probes from every kubelet and load balancer cost the dependency one call per ttl, not one each.
func NewReadiness ¶ added in v0.11.0
NewReadiness builds the check set; a zero ttl or timeout takes 5s and 2s.
type Services ¶
type Services struct {
// Ready is the deep dependency check behind /status/health; nil reports healthy.
Ready *Readiness
Authz *authz.Client
Entitlements *entitlements.Client
// OwnerLimits records the owner's entitlement values for integration credentials, which carry
// no user bearer. Optional; without it a credential's limit checks fail closed.
OwnerLimits *data.OwnerEntitlements
Themes *domain.Themes
Credentials *domain.Credentials
Limits *domain.Limits
Locks *domain.Locks
Rosters *domain.Rosters
Encounters *domain.Encounters
Sessions *domain.Sessions
// ChangeRequests handles player-submitted combatant changes; it needs Encounters and Sessions.
ChangeRequests *domain.ChangeRequests
Systems *data.SystemRepo
// Relations answers session authorization checks (PADR-0035). Without it every session
// request fails closed with 503.
Relations *relations.Client
// Streams and Hub serve the event stream; without both there is no /streams route. Stream
// holds its transport settings.
Streams *domain.Streams
Hub *stream.Hub
// NotificationPrefs holds each member's muted notice types per session.
NotificationPrefs *domain.NotificationPrefs
Stream StreamOptions
// Leases counts a user's open streams across pods; without it the per-user cap is not applied.
Leases *data.StreamLeases
}
Services are the dependencies the resource handlers need.
type StreamOptions ¶ added in v0.10.0
type StreamOptions struct {
// AllowedOrigins are host patterns (path.Match) a browser's Origin may match; no Origin header
// is always allowed.
AllowedOrigins []string
// MaxAge ends a stream after this long (default 1h); Heartbeat is the interval of the
// application heartbeat and the protocol ping (default 20s).
MaxAge, Heartbeat time.Duration
// RecheckInterval is how often a stream re-reads its membership and credential (default 5s,
// never above Heartbeat).
RecheckInterval time.Duration
// Buffer is how many events a stream may fall behind before it is closed (default 64).
Buffer int
// ConcurrentMax is the most streams one user may hold (Services.Leases counts them, with
// seats lasting LeaseTTL unless renewed and kept Grace after a drop); MaxPerPod is this pod's
// cap, answered with close code 1013. Zero disables the cap.
ConcurrentMax, CredentialMax, MaxPerPod int
// DevicesMax is the most connections one member may hold to a session (zero: the default).
DevicesMax int
LeaseTTL, Grace time.Duration
// Pod names this pod on its seats.
Pod string
// contains filtered or unexported fields
}
StreamOptions are the transport settings of the event stream.
func (StreamOptions) FaultBound ¶ added in v0.14.0
func (o StreamOptions) FaultBound() time.Duration
FaultBound is RevocationBound when the store cannot answer the re-checks: the stream closes at the third consecutive failed one, so each of three intervals may also spend a full check timeout.
func (StreamOptions) RevocationBound ¶ added in v0.14.0
func (o StreamOptions) RevocationBound() time.Duration
RevocationBound is the longest a stream stays open after its member is removed, demoted or blocked, when the eviction itself is lost (the evictor or the relay is down) and the client keeps up: one re-check interval to the next re-check, plus the time that check may take.
func (StreamOptions) StalledBound ¶ added in v0.14.0
func (o StreamOptions) StalledBound() time.Duration
StalledBound is RevocationBound when the client's socket is slow enough to hold the loop for a whole heartbeat write and renewal first.
func (StreamOptions) StalledFaultBound ¶ added in v0.14.0
func (o StreamOptions) StalledFaultBound() time.Duration
StalledFaultBound is FaultBound with a slow client.
Source Files
¶
- bind.go
- change_requests.go
- correlation.go
- credentials.go
- encounter_views.go
- encounters.go
- entitlements.go
- errors.go
- game_systems.go
- handlers.go
- limits.go
- limits_create.go
- middleware.go
- notification_prefs.go
- origin.go
- query.go
- ratelimit.go
- refs.go
- revision.go
- rosters.go
- sessions.go
- status.go
- streams.go
- themes.go
- typederrors.go