Documentation
¶
Overview ¶
Package handlers provides middleware `http.Handler` handlers for use with `server.Server` implementations.
Index ¶
- Constants
- func DisabledHandler(disabled bool, next http.Handler) http.Handler
- func NotImplementedHandler() http.Handler
- func NullHandler() http.Handler
- func PingHandler(response string) (http.Handler, error)
- func PingPongHandler() (http.Handler, error)
- func RouteHandler(handlers map[string]RouteHandlerFunc) (http.Handler, error)
- func RouteHandlerWithOptions(opts *RouteHandlerOptions) (http.Handler, error)
- type OpenSearchDescription
- type OpenSearchImage
- type OpenSearchURL
- type OpenSearchURLParameter
- type RouteHandlerFunc
- type RouteHandlerOptions
Constants ¶
const DEFAULT_IMAGE_HEIGHT int = 16
const DEFAULT_IMAGE_URI string = "" /* 5617-byte string literal not displayed */
const DEFAULT_IMAGE_WIDTH int = 16
const DEFAULT_SEARCHTERMS string = "{searchTerms}"
const DEFAULT_URL_METHOD string = "GET"
const DEFAULT_URL_TYPE string = "text/html"
const NS_MOZ string = "http://www.mozilla.org/2006/browser/search/"
const NS_OPENSEARCH string = "http://a9.com/-/spec/opensearch/1.1/"
Variables ¶
This section is empty.
Functions ¶
func DisabledHandler ¶
DisabledHandler is a middleware handler that returns an HTTP 503 (Service unavailable) error if 'disabled' is true. Otherwise it serves 'next'.
func NotImplementedHandler ¶
func NullHandler ¶
func PingPongHandler ¶
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 OpenSearchDescription ¶
type OpenSearchDescription struct { XMLName xml.Name `xml:"OpenSearchDescription"` NSMoz string `xml:"xmlns:moz,attr"` InputEncoding string `xml:"InputEncoding"` NSOpenSearch string `xml:"xmlns,attr"` ShortName string `xml:"ShortName"` Description string `xml:"Description"` Image *OpenSearchImage `xml:"Image"` URL *OpenSearchURL `xml:"Url"` SearchForm string `xml:"moz:searchForm"` }
func (*OpenSearchDescription) Marshal ¶
func (d *OpenSearchDescription) Marshal() ([]byte, error)
type OpenSearchImage ¶
type OpenSearchURL ¶
type OpenSearchURL struct { Type string `xml:"type,attr"` Method string `xml:"method,attr"` Template string `xml:"template,attr"` Parameters []*OpenSearchURLParameter `xml:"Param"` }
type OpenSearchURLParameter ¶
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.