handlers

package
v3.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2025 License: BSD-3-Clause Imports: 9 Imported by: 1

Documentation

Overview

Package handlers provides middleware `http.Handler` handlers for use with `server.Server` implementations.

Index

Constants

View Source
const DEFAULT_IMAGE_HEIGHT int = 16
View Source
const DEFAULT_IMAGE_URI string = "" /* 5617-byte string literal not displayed */
View Source
const DEFAULT_IMAGE_WIDTH int = 16
View Source
const DEFAULT_SEARCHTERMS string = "{searchTerms}"
View Source
const DEFAULT_URL_METHOD string = "GET"
View Source
const DEFAULT_URL_TYPE string = "text/html"
View Source
const NS_MOZ string = "http://www.mozilla.org/2006/browser/search/"
View Source
const NS_OPENSEARCH string = "http://a9.com/-/spec/opensearch/1.1/"

Variables

This section is empty.

Functions

func DisabledHandler

func DisabledHandler(disabled bool, next http.Handler) http.Handler

DisabledHandler is a middleware handler that returns an HTTP 503 (Service unavailable) error if 'disabled' is true. Otherwise it serves 'next'.

func NotImplementedHandler

func NotImplementedHandler() http.Handler

func NullHandler

func NullHandler() http.Handler

func PingHandler

func PingHandler(response string) (http.Handler, error)

func PingPongHandler

func PingPongHandler() (http.Handler, error)

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:

  1. 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.
  2. 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 OpenSearchImage struct {
	Height int    `xml:"height,attr"`
	Width  int    `xml:"width,attr"`
	URI    string `xml:",chardata"`
}

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 OpenSearchURLParameter struct {
	Name  string `xml:"name,attr"`
	Value string `xml:"value,attr"`
}

type RouteHandlerFunc

type RouteHandlerFunc func(context.Context) (http.Handler, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL