wrappers

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2019 License: BSD-2-Clause Imports: 4 Imported by: 0

README

wrappers

Yet another Go http decorator package. This time with feeling.

Requires go.uuid v1.2.0

...
authenwrapper := wrappers.NewAuthenticationWrapper([]auth.Authenticator{authenticate})
jsonwrapper := wrappers.NewContentTypeWrapper("application/json")
htmlwrapper := wrappers.NewContentTypeWrapper("text/html")
tidwrapper := wrappers.TIDWrapper

apiPost := wrappers.Wrap(api.PostHandler(), tidwrapper, authenwrapper, jsonwrapper)
root := wrappers.Wrap(handlers.StaticHandler(wwwRoot), tidwrapper, htmlwrapper)

m := http.NewServeMux()
m.HandleFunc("/", root)
m.HandleFunc("/api", apiPost)

log.Print(http.ListenAndServe(":80", m))
...

Documentation

Overview

Package wrappers provides tools to wrap http.handlerFuncs

Index

Constants

View Source
const (
	// AuthorizorContextKey is the key used to store and retrieve an authorizor in the context
	AuthorizorContextKey authorizorContextKey = 1
)
View Source
const (
	// TIDContextKey is the key used to store a TID in a context. See https://blog.golang.org/context
	TIDContextKey contextKey = 0
)

Variables

View Source
var (
	// TransIDHeader is the header name for transaction IDs.
	TransIDHeader = "X-SITE-TRANS-ID"
)

Functions

func AuthorizationWrapper

func AuthorizationWrapper(next http.HandlerFunc) http.HandlerFunc

AuthorizationWrapper is a wrapper which will authorize requests on the provided endpoint

func TIDWrapper

func TIDWrapper(next http.HandlerFunc) http.HandlerFunc

TIDWrapper creates a transaction ID for any request if not set. Generaly, this should be the first decorator applied

func Wrap

func Wrap(handler http.HandlerFunc, wrappers ...Wrapper) http.HandlerFunc

Wrap an http.HandlerFunc with 0 or more Wrappers. The order of the wrappers is from outermost to innermost wrapper, or the order in which they will be called on a request.

Types

type Wrapper

type Wrapper func(http.HandlerFunc) http.HandlerFunc

Wrapper is an http handler function decorater

func NewAuthenticationWrapper

func NewAuthenticationWrapper(authens []auth.Authenticator) Wrapper

NewAuthenticationWrapper creates a wrapper which will authenticate an endpoint using the supplied authenticators

func NewContentTypeWrapper

func NewContentTypeWrapper(contentType string) Wrapper

NewContentTypeWrapper returns a wrapper which sets the content type for the request

Jump to

Keyboard shortcuts

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