Documentation ¶
Overview ¶
Package adaptd provides a simple adapter interface for adding middleware to http frameworks
Index ¶
- func Adapt(h http.Handler, adapters ...Adapter) http.Handler
- func HTTPSRedirect(port string) http.Handler
- type Adapter
- func AddCookieWithFunc(name string, tg func(http.ResponseWriter, *http.Request) error) Adapter
- func AddHeader(name, value string) Adapter
- func AddHeaderWithFunc(name string, tg func() string) Adapter
- func CheckAndRedirect(f HandlerChecker, redirect http.Handler, logOnRedirect string) Adapter
- func CountHTTPResponses() Adapter
- func DisallowLongerPaths(path string, notFoundHandler http.Handler) Adapter
- func EnsureHTTPS(allowXForwardedProto bool) Adapter
- func GetAndOtherRequest(other http.Handler, method string) Adapter
- func Notify(logger *log.Logger) Adapter
- func OnCheck(f HandlerChecker, falseHandler http.Handler, logOnFalse string) Adapter
- func RequestMethod(method string) Adapter
- func TrackHTTPResponseTimes() Adapter
- type HandlerChecker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Adapt ¶
Adapt is a helper to add all the adapters required for a given http.Handler. Adapters will be called in the order they are given when the returned http.Handler is called.
func HTTPSRedirect ¶
HTTPSRedirect adapter redirects all HTTP requests to HTTPS requests. Most users should simply call this as go http.ListenAndServe(":80", HTTPSRedirect("443"))
Types ¶
type Adapter ¶
Adapter is a type that helps with http middleware.
func AddCookieWithFunc ¶
AddCookieWithFunc adds the header before calling the handler. This is useful for things like CSRF tokens.
func AddHeaderWithFunc ¶
AddHeaderWithFunc adds the header before calling the handler. This is useful for things like CSRF tokens.
func CheckAndRedirect ¶
func CheckAndRedirect(f HandlerChecker, redirect http.Handler, logOnRedirect string) Adapter
CheckAndRedirect adapter checks the return of the function. On false, it redirects to the given URL. On true, it will call the handler passed to the Adapater.
func CountHTTPResponses ¶ added in v1.0.2
func CountHTTPResponses() Adapter
CountHTTPResponses calls the handler and records the response as a prometheus counter with labels endpoint, code, and method. This should be applied once for an entire web server.
func DisallowLongerPaths ¶
DisallowLongerPaths adapter calls the notFoundHandler if the URL path is longer than the registered one. For example, paths that do not match any registered handler are sent to the handler for "/". Adding this Adapter could display at custom 404 page.
func EnsureHTTPS ¶
EnsureHTTPS adapter redirects an HTTP request to an HTTPS request. Some hosts forward requests and use 'X-Forward-Proto == "https"' to indicate that he request was made with https protocol. If you would like to allow this as a valid check, then the parameter should be true.
func GetAndOtherRequest ¶
GetAndOtherRequest adapter uses two handlers to handle both get requests and another. All other requests are given a http.StatusMethodNotAllowed error. The other handler is provided to create the Adapter while the get handler should be provided to the Adapter e.g. GetAndOtherRequest(other, http.MethodPost)(getHandler)
func Notify ¶
Notify adapter logs when the request is beginning to be processed and when it is finished.
func OnCheck ¶
func OnCheck(f HandlerChecker, falseHandler http.Handler, logOnFalse string) Adapter
OnCheck adapter checks the return of the function. On false, it calls the handler. On true, it will call the handler passed to the Adapter.
func RequestMethod ¶
RequestMethod adapter allow allows the given request method. All other requests are given a http.StatusMethodNotAllowed error.
func TrackHTTPResponseTimes ¶ added in v1.0.2
func TrackHTTPResponseTimes() Adapter
TrackHTTPResponseTimes calls the handler and records the response time as a prometheus summary with labels endpoint, code, and method. This should be applied once for an entire web server.
type HandlerChecker ¶
type HandlerChecker func(http.ResponseWriter, *http.Request) bool
HandlerChecker checks the *http.Request and allows an Adapter to use different Handlers based on its return. For example, a HandlerChecker can check if a user is logged in when the login page is visited. If no user is logged in, the login page is shown. If a user is already logged in, then the Adapter might redirect to another page.