Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RouteHandler ¶
func RouteHandler(handlers map[string]RouteHandlerFunc) (http.Handler, error)
RouteHandler create a new `http.Handler` instance that will serve requests using handlers defined in 'handlers'.
func RouteHandlerWithOptions ¶
func RouteHandlerWithOptions(opts *RouteHandlerOptions) (http.Handler, error)
RouteHandlerWithOptions create a new `http.Handler` instance that will serve requests using handlers defined in 'opts.Handlers'. This is essentially a "middleware" handler than does all the same routing that the default `http.ServeMux` handler does but defers initiating the handlers being routed to until they invoked at runtime. Only one handler is initialized (or retrieved from an in-memory cache) and served for any given path being by a `RouteHandler` request.
The reason this handler exists is for web applications that:
- Are deployed as AWS Lambda functions (with an API Gateway integration) using the "lambda://" `server.Server` implementation that have more handlers than you need or want to initiate, but never use, for every request.
- You don't want to refactor in to (n) atomic Lambda functions. That is you want to be able to re-use the same code in both a plain-vanilla HTTP server configuration as well as Lambda + API Gateway configuration.
URL patterns for handlers (passed in the `RouteHandlerOptions` struct) are parsed using a custom implementation of Go 1.22's "URL pattern matching". While this implementation has been tested on common patterns it is likely that there are still edge-cases, or simply sufficiently sophisticated cases, that will not match. This is considered a "known known" and those cases will need to be addressed as they arise. This custom implementation uses regular expressions and has not been benchmarked against the Go 1.22 implementation. I would prefer to use the native implementation but it is a) code that is private to net/http and sufficiently involved that cloning it in to this package, and then tracking the changes, seems like a bad idea.
Types ¶
type RouteHandlerFunc ¶
RouteHandlerFunc returns an `http.Handler` instance.
type RouteHandlerOptions ¶
type RouteHandlerOptions struct {
// Handlers is a map whose keys are `http.ServeMux` style routing patterns and whose keys
// are functions that when invoked return `http.Handler` instances.
Handlers map[string]RouteHandlerFunc
}
RouteHandlerOptions is a struct that contains configuration settings for use the RouteHandlerWithOptions method.