net

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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.

func SetSecureCookie

func SetSecureCookie(c *gin.Context, name, value string, maxAge int, path string, httpOnly bool)

SetSecureCookie sets a cookie with Secure auto-detected from the request's TLS state and appends SameSite=Lax. Use this instead of c.SetCookie for security-sensitive cookies.

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

type Server struct {
	*http.Server
	// contains filtered or unexported fields
}

Server wraps an http.Server with TLS and lifecycle management.

func NewServer

func NewServer(handler http.Handler, addr string, cfg *TLSConfig) *Server

NewServer creates a production-ready HTTP server.

func (*Server) Close

func (s *Server) Close() error

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

func (s *Server) ListenAndServe() error

ListenAndServe starts the server with TLS (if configured) or plain HTTP.

func (*Server) ListenAndServeReady

func (s *Server) ListenAndServeReady(onReady func()) error

ListenAndServeReady binds the socket before invoking onReady, allowing the caller to publish readiness and startup measurements only after the server can actually accept connections.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully stops the main server and the owned HTTP redirect server, joining the latter before returning.

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

func (sr *SiteRouter) EnsureName(ctx context.Context, name string) (known bool, err error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL