Documentation
¶
Overview ¶
Package net provides the Go-native HTTP server with TLS, multi-site routing, and all production middleware (security headers, CORS, rate limiting, CSRF).
Index ¶
- func CORSMiddleware(allowedOrigins []string) gin.HandlerFunc
- func CompressMiddleware() gin.HandlerFunc
- func InjectSiteRuntimeContext(c *gin.Context, snapshot *tenantruntime.Snapshot)
- func RegisterPathSiteRoutes(router *gin.Engine, sr *SiteRouter)
- func RequestIDMiddleware() gin.HandlerFunc
- func SecurityHeadersMiddleware(tlsEnabled bool) gin.HandlerFunc
- func SetSecureCookie(c *gin.Context, name, value string, maxAge int, path string, httpOnly bool)
- type LoadedSite
- type RateLimiter
- type RuntimeSnapshot
- type Server
- type SiteRouter
- func (sr *SiteRouter) AcquireByHost(ctx context.Context, host string, fallback bool) (*LoadedSite, *tenantruntime.Handle, bool, bool)
- func (sr *SiteRouter) AcquireByID(ctx context.Context, siteID string) (*tenantruntime.Handle, error)
- func (sr *SiteRouter) AcquireByName(ctx context.Context, name string) (*LoadedSite, *tenantruntime.Handle, bool)
- func (sr *SiteRouter) AddSite(site *LoadedSite) error
- func (sr *SiteRouter) AdvanceDirectoryRevision(siteID string, revision uint64) error
- func (sr *SiteRouter) AllDomains() []string
- func (sr *SiteRouter) AllSites() []*LoadedSite
- func (sr *SiteRouter) AllSnapshots() []*RuntimeSnapshot
- func (sr *SiteRouter) ApplySiteDescriptor(descriptor contract.SiteDescriptor) error
- func (sr *SiteRouter) ApplySiteStatus(siteID, status string, revision uint64) error
- func (sr *SiteRouter) EnsureHost(ctx context.Context, host string, fallback bool) (known bool, err error)
- func (sr *SiteRouter) EnsureName(ctx context.Context, name string) (known bool, err error)
- func (sr *SiteRouter) EvictLeastRecentlyUsedIdle(ctx context.Context) (*LoadedSite, error)
- func (sr *SiteRouter) FinishRetirement(site *LoadedSite)
- func (sr *SiteRouter) IsAmbiguousHost(host string) bool
- func (sr *SiteRouter) IsLazyAliasAmbiguous(host string) bool
- func (sr *SiteRouter) Middleware() gin.HandlerFunc
- func (sr *SiteRouter) PinExistingByID(ctx context.Context, siteID string) (*LoadedSite, *tenantruntime.Handle, error)
- func (sr *SiteRouter) RemoveLazyDescriptor(siteID string)
- func (sr *SiteRouter) RemoveSite(name string) *LoadedSite
- func (sr *SiteRouter) RemoveSiteByID(siteID string) *LoadedSite
- func (sr *SiteRouter) RemoveSiteByIDContext(ctx context.Context, siteID string) (*LoadedSite, error)
- func (sr *SiteRouter) ReplaceSite(site *LoadedSite) (*LoadedSite, error)
- func (sr *SiteRouter) ReplaceSiteContext(ctx context.Context, site *LoadedSite) (*LoadedSite, error)
- func (sr *SiteRouter) ResidentCapacity() int
- func (sr *SiteRouter) ResourceCounts() (active, retiring int)
- func (sr *SiteRouter) RetireUnavailable(ctx context.Context, siteID string) (*LoadedSite, error)
- func (sr *SiteRouter) RuntimeManager() *tenantruntime.Manager
- func (sr *SiteRouter) SetLazyActivator(activate func(context.Context, string) error)
- func (sr *SiteRouter) SetLazyDirectory(descriptors []contract.SiteDescriptor, ...)
- func (sr *SiteRouter) SiteByHost(host string) (*LoadedSite, bool)
- func (sr *SiteRouter) SiteByID(siteID string) *LoadedSite
- func (sr *SiteRouter) SiteByName(name string) *LoadedSite
- func (sr *SiteRouter) SnapshotByID(siteID string) *RuntimeSnapshot
- func (sr *SiteRouter) SnapshotByName(name string) *RuntimeSnapshot
- func (sr *SiteRouter) UpdateLazyDescriptor(descriptor contract.SiteDescriptor)
- func (sr *SiteRouter) UpdateSiteConfig(name string, config SiteRouterConfig) *LoadedSite
- func (sr *SiteRouter) ValidateAliases() error
- func (sr *SiteRouter) WaitForTenantIdle(ctx context.Context, siteID string) error
- type SiteRouterConfig
- type TLSConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CORSMiddleware ¶
func CORSMiddleware(allowedOrigins []string) gin.HandlerFunc
CORSMiddleware configures cross-origin access. When allowedOrigins is empty (dev mode), origins are reflected from the request. This is safe with credentials because the origin is reflected, not wildcarded. In production, always pass explicit allowed origins.
func CompressMiddleware ¶
func CompressMiddleware() gin.HandlerFunc
CompressMiddleware returns a Gin middleware that gzips HTTP responses when the client sends Accept-Encoding: gzip. The compress decision is made lazily per response (see gzipResponseWriter) so handlers that set Content-Length, answer Range or HEAD requests, stream events, or return tiny or already-compressed bodies are never given a mismatched or encoded body.
func InjectSiteRuntimeContext ¶
func InjectSiteRuntimeContext(c *gin.Context, snapshot *tenantruntime.Snapshot)
InjectSiteRuntimeContext restores the tenant-scoped request state from the pinned runtime snapshot. Shared path-based consumers such as the embedded workspace use it before re-dispatching a request through Gin.
func RegisterPathSiteRoutes ¶
func RegisterPathSiteRoutes(router *gin.Engine, sr *SiteRouter)
RegisterPathSiteRoutes adds a NoRoute handler that intercepts /s/:site/* requests, injects site context, rewrites the path, and re-dispatches API calls. This allows multi-site access without DNS or /etc/hosts.
localhost:8000/s/fieldwork/api/... → fieldwork site
The Engine serves no UI files; anything else under /s/:site returns 404.
func RequestIDMiddleware ¶
func RequestIDMiddleware() gin.HandlerFunc
RequestIDMiddleware ensures every request has a traceable ID.
func SecurityHeadersMiddleware ¶
func SecurityHeadersMiddleware(tlsEnabled bool) gin.HandlerFunc
SecurityHeadersMiddleware sets production security headers via gin-contrib/secure.
Types ¶
type LoadedSite ¶
type LoadedSite = tenantruntime.Runtime
LoadedSite and SiteRouterConfig are compatibility aliases for the tenant runtime model owned by internal/tenantruntime.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements per-user token bucket rate limiting.
func NewRateLimiter ¶
func NewRateLimiter(rps float64, burst int) *RateLimiter
NewRateLimiter creates a rate limiter. rps: requests per second allowed per user. burst: max burst size.
func (*RateLimiter) Middleware ¶
func (rl *RateLimiter) Middleware() gin.HandlerFunc
Middleware returns a Gin middleware that rate-limits requests per user.
type RuntimeSnapshot ¶
type RuntimeSnapshot = tenantruntime.Snapshot
type Server ¶
Server wraps an http.Server with TLS and lifecycle management.
func (*Server) Close ¶
Close force-closes both listeners. The redirect serve loop is joined before returning so callers have one shutdown boundary for the complete server.
func (*Server) ListenAndServe ¶
ListenAndServe starts the server with TLS (if configured) or plain HTTP.
func (*Server) ListenAndServeReady ¶
ListenAndServeReady binds the socket before invoking onReady, allowing the caller to publish readiness and startup measurements only after the server can actually accept connections.
type SiteRouter ¶
type SiteRouter struct {
// contains filtered or unexported fields
}
SiteRouter is the HTTP facade over the tenant runtime manager. It owns no competing tenant registry or generation state.
func NewSiteRouter ¶
func NewSiteRouter(sites []*LoadedSite) *SiteRouter
func NewSiteRouterWithRetirementLimit ¶
func NewSiteRouterWithRetirementLimit(sites []*LoadedSite, maxRetiring uint16, maxResident ...uint32) (*SiteRouter, error)
func (*SiteRouter) AcquireByHost ¶
func (sr *SiteRouter) AcquireByHost(ctx context.Context, host string, fallback bool) (*LoadedSite, *tenantruntime.Handle, bool, bool)
func (*SiteRouter) AcquireByID ¶
func (sr *SiteRouter) AcquireByID(ctx context.Context, siteID string) (*tenantruntime.Handle, error)
func (*SiteRouter) AcquireByName ¶
func (sr *SiteRouter) AcquireByName(ctx context.Context, name string) (*LoadedSite, *tenantruntime.Handle, bool)
func (*SiteRouter) AddSite ¶
func (sr *SiteRouter) AddSite(site *LoadedSite) error
func (*SiteRouter) AdvanceDirectoryRevision ¶
func (sr *SiteRouter) AdvanceDirectoryRevision(siteID string, revision uint64) error
func (*SiteRouter) AllDomains ¶
func (sr *SiteRouter) AllDomains() []string
func (*SiteRouter) AllSites ¶
func (sr *SiteRouter) AllSites() []*LoadedSite
func (*SiteRouter) AllSnapshots ¶
func (sr *SiteRouter) AllSnapshots() []*RuntimeSnapshot
func (*SiteRouter) ApplySiteDescriptor ¶
func (sr *SiteRouter) ApplySiteDescriptor(descriptor contract.SiteDescriptor) error
func (*SiteRouter) ApplySiteStatus ¶
func (sr *SiteRouter) ApplySiteStatus(siteID, status string, revision uint64) error
func (*SiteRouter) EnsureHost ¶
func (sr *SiteRouter) EnsureHost(ctx context.Context, host string, fallback bool) (known bool, err error)
EnsureHost activates a known cold tenant before routing. It returns known false when the host is not in the Engine's credential-free directory view.
func (*SiteRouter) EnsureName ¶
EnsureName activates a known cold tenant selected by its stable ID or alias.
func (*SiteRouter) EvictLeastRecentlyUsedIdle ¶
func (sr *SiteRouter) EvictLeastRecentlyUsedIdle(ctx context.Context) (*LoadedSite, error)
func (*SiteRouter) FinishRetirement ¶
func (sr *SiteRouter) FinishRetirement(site *LoadedSite)
func (*SiteRouter) IsAmbiguousHost ¶
func (sr *SiteRouter) IsAmbiguousHost(host string) bool
func (*SiteRouter) IsLazyAliasAmbiguous ¶
func (sr *SiteRouter) IsLazyAliasAmbiguous(host string) bool
func (*SiteRouter) Middleware ¶
func (sr *SiteRouter) Middleware() gin.HandlerFunc
Middleware returns a Gin middleware that resolves the Host header to a site.
func (*SiteRouter) PinExistingByID ¶
func (sr *SiteRouter) PinExistingByID(ctx context.Context, siteID string) (*LoadedSite, *tenantruntime.Handle, error)
func (*SiteRouter) RemoveLazyDescriptor ¶
func (sr *SiteRouter) RemoveLazyDescriptor(siteID string)
func (*SiteRouter) RemoveSite ¶
func (sr *SiteRouter) RemoveSite(name string) *LoadedSite
func (*SiteRouter) RemoveSiteByID ¶
func (sr *SiteRouter) RemoveSiteByID(siteID string) *LoadedSite
func (*SiteRouter) RemoveSiteByIDContext ¶
func (sr *SiteRouter) RemoveSiteByIDContext(ctx context.Context, siteID string) (*LoadedSite, error)
func (*SiteRouter) ReplaceSite ¶
func (sr *SiteRouter) ReplaceSite(site *LoadedSite) (*LoadedSite, error)
func (*SiteRouter) ReplaceSiteContext ¶
func (sr *SiteRouter) ReplaceSiteContext(ctx context.Context, site *LoadedSite) (*LoadedSite, error)
func (*SiteRouter) ResidentCapacity ¶
func (sr *SiteRouter) ResidentCapacity() int
func (*SiteRouter) ResourceCounts ¶
func (sr *SiteRouter) ResourceCounts() (active, retiring int)
func (*SiteRouter) RetireUnavailable ¶
func (sr *SiteRouter) RetireUnavailable(ctx context.Context, siteID string) (*LoadedSite, error)
func (*SiteRouter) RuntimeManager ¶
func (sr *SiteRouter) RuntimeManager() *tenantruntime.Manager
func (*SiteRouter) SetLazyActivator ¶
func (sr *SiteRouter) SetLazyActivator(activate func(context.Context, string) error)
func (*SiteRouter) SetLazyDirectory ¶
func (sr *SiteRouter) SetLazyDirectory(descriptors []contract.SiteDescriptor, activate func(context.Context, string) error)
SetLazyDirectory installs the credential-free alias projection used to activate a tenant only after it receives routed work. The activator fetches tenant credentials from the authoritative Engine directory for one ID.
func (*SiteRouter) SiteByHost ¶
func (sr *SiteRouter) SiteByHost(host string) (*LoadedSite, bool)
func (*SiteRouter) SiteByID ¶
func (sr *SiteRouter) SiteByID(siteID string) *LoadedSite
func (*SiteRouter) SiteByName ¶
func (sr *SiteRouter) SiteByName(name string) *LoadedSite
func (*SiteRouter) SnapshotByID ¶
func (sr *SiteRouter) SnapshotByID(siteID string) *RuntimeSnapshot
func (*SiteRouter) SnapshotByName ¶
func (sr *SiteRouter) SnapshotByName(name string) *RuntimeSnapshot
func (*SiteRouter) UpdateLazyDescriptor ¶
func (sr *SiteRouter) UpdateLazyDescriptor(descriptor contract.SiteDescriptor)
UpdateLazyDescriptor advances the credential-free projection without changing a currently published runtime generation.
func (*SiteRouter) UpdateSiteConfig ¶
func (sr *SiteRouter) UpdateSiteConfig(name string, config SiteRouterConfig) *LoadedSite
func (*SiteRouter) ValidateAliases ¶
func (sr *SiteRouter) ValidateAliases() error
func (*SiteRouter) WaitForTenantIdle ¶
func (sr *SiteRouter) WaitForTenantIdle(ctx context.Context, siteID string) error
type SiteRouterConfig ¶
type SiteRouterConfig = tenantruntime.SiteConfig
type TLSConfig ¶
type TLSConfig struct {
Mode string // "auto" (Let's Encrypt), "manual" (cert files), "off" (HTTP only)
Domains []string // All domains across all sites (for autocert whitelist)
Email string // Contact email for Let's Encrypt
CertDir string // Directory to cache certs (default: "certs")
}
TLSConfig controls TLS behavior.