Documentation
¶
Overview ¶
Package httpserver provides a reusable HTTP server implementation with common functionality for TEE registry system components.
The httpserver package implements a base HTTP server with standard health endpoints, graceful shutdown capabilities, metrics, and flexible routing. This allows different components of the TEE registry system to reuse common server functionality while implementing their specific endpoints.
Key Components ¶
- BaseServer: Core HTTP server with health checks, metrics, and lifecycle management
- RouteRegistrar: Interface for components to register their routes with the server
Server Lifecycle ¶
The BaseServer implements a complete server lifecycle:
- Initialization: Configure server with HTTP settings and route registrars
- Startup: Run HTTP and metrics servers in background goroutines
- Operation: Handle requests with proper logging and monitoring
- Readiness Control: Support drain/undrain operations for load balancers
- Graceful Shutdown: Wait for in-flight requests to complete
Health and Diagnostics ¶
All servers built with BaseServer automatically include:
- Liveness Check: Simple endpoint to verify server is running (/livez)
- Readiness Check: Endpoint indicating if server is ready to accept requests (/readyz)
- Drain Control: Endpoints to prepare for graceful shutdown (/drain, /undrain)
- Metrics: Optional Prometheus-compatible metrics endpoint
- Profiling: Optional pprof debugging endpoints when enabled
Usage Example ¶
// Implement the RouteRegistrar interface for your handler
func (h *MyHandler) RegisterRoutes(r chi.Router) {
r.Get("/api/resource/{id}", h.HandleGetResource)
r.Post("/api/resource", h.HandleCreateResource)
}
// Create your specialized server with the base server embedded
type MyServer struct {
*httpserver.BaseServer
handler *MyHandler
}
// Create a new server instance
func NewMyServer(cfg *api.HTTPServerConfig) (*MyServer, error) {
handler := NewMyHandler()
// Create base server with the handler as a route registrar
baseServer, err := httpserver.New(cfg, handler)
if err != nil {
return nil, err
}
return &MyServer{
BaseServer: baseServer,
handler: handler,
}, nil
}
// Use the server
srv, _ := NewMyServer(config)
srv.RunInBackground()
defer srv.Shutdown()
This approach ensures consistent behavior across different server types while allowing specialized functionality to be easily added.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseServer ¶
type BaseServer struct {
// contains filtered or unexported fields
}
BaseServer provides common HTTP server functionality for different components of the TEE registry system.
func New ¶
func New(cfg *HTTPServerConfig, routeRegistrars ...RouteRegistrar) (*BaseServer, error)
New creates a new BaseServer with the specified configuration.
Parameters:
- cfg: Server configuration
- routeRegistrars: Components that will register routes with the server
Returns:
- Configured server instance
- Error if server creation fails
func (*BaseServer) RunInBackground ¶
func (srv *BaseServer) RunInBackground()
RunInBackground starts the HTTP and metrics servers in separate goroutines.
func (*BaseServer) Shutdown ¶
func (srv *BaseServer) Shutdown()
Shutdown gracefully stops the HTTP and metrics servers.
type ClientHandler ¶
type ClientHandler struct {
Client *client.ClientImpl
}
func NewClientHandler ¶
func NewClientHandler(Client *client.ClientImpl) *ClientHandler
func (*ClientHandler) RegisterRoutes ¶
func (h *ClientHandler) RegisterRoutes(r chi.Router)
func (*ClientHandler) RunInBackground ¶
func (h *ClientHandler) RunInBackground()
type HTTPServerConfig ¶
type HTTPServerConfig struct {
// ListenAddr is the address and port the HTTP server will listen on.
ListenAddr string
// MetricsAddr is the address and port for the metrics server.
// If empty, metrics server will not be started.
MetricsAddr string
// EnablePprof enables the pprof debugging API when true.
EnablePprof bool
// Log is the structured logger for server operations.
Log *slog.Logger
// DrainDuration is the time to wait after marking server not ready
// before shutting down, allowing load balancers to detect the change.
DrainDuration time.Duration
// GracefulShutdownDuration is the maximum time to wait for in-flight
// requests to complete during shutdown.
GracefulShutdownDuration time.Duration
// ReadTimeout is the maximum duration for reading the entire request,
// including the body.
ReadTimeout time.Duration
// WriteTimeout is the maximum duration before timing out writes of
// the response.
WriteTimeout time.Duration
}
HTTPServerConfig contains all configuration parameters for the HTTP server.
type NetworkConfig ¶
See zipnet.ZIPNetConfig
type RouteRegistrar ¶
type RouteRegistrar interface {
// RegisterRoutes registers routes with the provided router
RegisterRoutes(r chi.Router)
}
RouteRegistrar defines the interface for components that register routes with the server's router.