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 required 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 must be 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 is a simple wrapper for 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. Using this option or WithConfig is required to initialize the Runner instance, because it provides the configuration for the HTTP server managed by the Runner.
func WithContext ¶
WithContext sets a custom context for the Runner instance. This allows for more granular control over cancellation and timeouts.
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 NewRoute ¶
NewRoute creates a new Route with the given name, path, and handler. The name must be unique, the path must be non-empty, and the handler must not be nil.
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 a configurable HTTP server that supports graceful shutdown, dynamic reconfiguration, and state monitoring. It meets 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.
type ServerCreator ¶
type ServerCreator func(addr string, handler http.Handler, cfg *Config) HttpServer
ServerCreator is a function type that creates an HttpServer instance