Documentation
¶
Overview ¶
Package httpserver provides a configurable, reloadable HTTP server implementation that can be managed by the supervisor package.
Index ¶
- Variables
- type Config
- type ConfigCallback
- type ConfigOption
- func WithConfigCopy(src *Config) ConfigOption
- func WithDrainTimeout(timeout time.Duration) ConfigOption
- func WithIdleTimeout(timeout time.Duration) ConfigOption
- func WithReadTimeout(timeout time.Duration) ConfigOption
- func WithRequestContext(ctx context.Context) ConfigOption
- func WithServerCreator(creator ServerCreator) ConfigOption
- func WithWriteTimeout(timeout time.Duration) ConfigOption
- type HttpServer
- type Option
- type Route
- type Routes
- type Runner
- type ServerCreator
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoConfig = errors.New("no config provided") ErrNoHandlers = errors.New("no handlers provided") ErrGracefulShutdown = errors.New("graceful shutdown failed") ErrGracefulShutdownTimeout = errors.New("graceful shutdown deadline reached") ErrHttpServer = errors.New("http server error") ErrOldConfig = errors.New("config hasn't changed since last update") ErrRetrieveConfig = errors.New("failed to retrieve server configuration") ErrCreateConfig = errors.New("failed to create server configuration") ErrServerNotRunning = errors.New("http server is not running") ErrServerReadinessTimeout = errors.New("server readiness check timed out") ErrServerBoot = errors.New("failed to start HTTP server") ErrConfigCallbackNil = errors.New("config callback returned nil") ErrConfigCallback = errors.New("failed to load configuration from callback") ErrStateTransition = errors.New("state transition failed") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Core configuration ListenAddr string DrainTimeout time.Duration Routes Routes // Server settings ReadTimeout time.Duration WriteTimeout time.Duration IdleTimeout time.Duration // Server creation callback function ServerCreator ServerCreator // contains filtered or unexported fields }
Config is the main configuration struct for the HTTP server
func NewConfig ¶
func NewConfig(addr string, routes Routes, opts ...ConfigOption) (*Config, error)
NewConfig creates a new Config with the address and routes plus any optional configuration via functional options
type ConfigCallback ¶
ConfigCallback is the function type signature for the callback used to load initial config, and new config during Reload()
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption defines a functional option for configuring Config
func WithConfigCopy ¶
func WithConfigCopy(src *Config) ConfigOption
WithConfigCopy creates a ConfigOption that copies most settings from the source config except for ListenAddr and Routes which are provided directly to NewConfig.
func WithDrainTimeout ¶
func WithDrainTimeout(timeout time.Duration) ConfigOption
WithDrainTimeout sets the drain timeout for graceful shutdown
func WithIdleTimeout ¶
func WithIdleTimeout(timeout time.Duration) ConfigOption
WithIdleTimeout sets the idle timeout for the HTTP server
func WithReadTimeout ¶
func WithReadTimeout(timeout time.Duration) ConfigOption
WithReadTimeout sets the read timeout for the HTTP server
func WithRequestContext ¶
func WithRequestContext(ctx context.Context) ConfigOption
WithRequestContext sets the context that will be propagated to all request handlers via http.Server's BaseContext. This allows handlers to be aware of server shutdown.
func WithServerCreator ¶
func WithServerCreator(creator ServerCreator) ConfigOption
WithServerCreator sets a custom server creator for the HTTP server
func WithWriteTimeout ¶
func WithWriteTimeout(timeout time.Duration) ConfigOption
WithWriteTimeout sets the write timeout for the HTTP server
type HttpServer ¶
HttpServer is the interface for the HTTP server
func DefaultServerCreator ¶
func DefaultServerCreator(addr string, handler http.Handler, cfg *Config) HttpServer
DefaultServerCreator creates a standard http.Server instance with the settings from Config
type Option ¶
type Option func(*Runner)
Option represents a functional option for configuring Runner.
func WithConfig ¶
WithConfig sets the initial configuration for the Runner instance. This option wraps the WithConfigCallback option, allowing you to pass a Config instance directly instead of a callback function. This is useful when you have a static configuration that doesn't require dynamic loading or reloading.
func WithConfigCallback ¶
func WithConfigCallback(callback ConfigCallback) Option
WithConfigCallback sets the function that will be called to load or reload configuration. Either this option or WithConfig initializes the Runner instance by providing the configuration for the HTTP server managed by the Runner.
func WithLogHandler ¶
WithLogHandler sets a custom slog handler for the Runner instance. For example, to use a custom JSON handler with debug level:
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}) runner, err := httpserver.NewRunner(ctx, httpserver.WithConfigCallback(configCallback), httpserver.WithLogHandler(handler))
type Route ¶
type Route struct { Path string Handler http.HandlerFunc // contains filtered or unexported fields }
Route represents a single HTTP route with a name, path, and handler function.
func NewRouteWithMiddleware ¶
func NewRouteWithMiddleware( name string, path string, handler http.HandlerFunc, middlewares ...middleware.Middleware, ) (*Route, error)
NewRouteWithMiddleware creates a new Route with the given name, path, handler, and applies the provided middlewares to the handler in the order they are provided.
func NewWildcardRoute ¶
func NewWildcardRoute( prefix string, handler http.HandlerFunc, middlewares ...middleware.Middleware, ) (*Route, error)
NewWildcardRoute creates a route that handles everything under /prefix/*. Clients calling /prefix/foo/bar will match this route.
type Routes ¶
type Routes []Route
Routes is a map of paths as strings, that route to http.HandlerFuncs
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner implements an HTTP server with graceful shutdown, dynamic reconfiguration, and state monitoring. It implements the Runnable, Reloadable, and Stateable interfaces from the supervisor package.
func (*Runner) GetStateChan ¶
GetStateChan returns a channel that emits the HTTP server's state whenever it changes. The channel is closed when the provided context is canceled.
func (*Runner) Reload ¶
func (r *Runner) Reload()
Reload refreshes the server configuration and restarts the HTTP server if necessary. This method is safe to call while the server is running and will handle graceful shutdown and restart.
func (*Runner) Run ¶
Run starts the HTTP server and handles its lifecycle. It transitions through FSM states and returns when the server is stopped or encounters an error.
type ServerCreator ¶
type ServerCreator func(addr string, handler http.Handler, cfg *Config) HttpServer
ServerCreator is a function type that creates an HttpServer instance
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package middleware provides HTTP middleware utilities for wrapping http.HandlerFunc with additional functionality such as logging, metrics, and response inspection.
|
Package middleware provides HTTP middleware utilities for wrapping http.HandlerFunc with additional functionality such as logging, metrics, and response inspection. |