handlers

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: MIT Imports: 4 Imported by: 5

README

Handlers

This package contains handlers to forward values coming from a request header or a cookie, to a request context.

The mapping is done using enumeration of possible keys, and their mappings to header, context or cookie keys.

Usage

  • Read a header from a request and add its value to the underlying request context:
    // Access token
    handler := handlers.CheckHeader(handlers.UserAccess)

    // Locale
    handler := handlers.CheckHeader(handlers.Locale)

    // CollectionID
    handler := handlers.CheckHeader(handlers.CollectionID)
  • Read a cookie and add its value to the output request context:
    // Access token
    handler := handlers.CheckCookie(handlers.UserAccess)

    // Locale
    handler := handlers.CheckCookie(handlers.Locale)

    // CollectionID
    handler := handlers.CheckCookie(handlers.CollectionID)
  • Access the context value:
    // Access token
    accessToken := ctx.Value(handlers.UserAccess.Context())

    // Locale
    locale := ctx.Value(handlers.Locale.Context())

    // CollectionID
    collectionID := ctx.Value(handlers.CollectionID.Context())

Documentation

Index

Constants

This section is empty.

Variables

KeyMaps maps the possible values of Key enumeration to their Header, Cookie and Context correspnding keys

Functions

func CheckCookie

func CheckCookie(key Key) func(http.Handler) http.Handler

CheckCookie is a wrapper which adds a cookie value (if found) to the request context. This function complies with alice middleware Constructor type: func(http.Handler) -> (http.Handler))

func CheckHeader

func CheckHeader(key Key) func(http.Handler) http.Handler

CheckHeader is a wrapper which adds a value from the request header (if found) to the request context. This function complies with alice middleware Constructor type: func(http.Handler) -> (http.Handler)

func DoCheckCookie added in v1.0.4

func DoCheckCookie(h http.Handler, key Key) http.Handler

DoCheckCookie returns a handler that performs the CheckCookie logic

func DoCheckHeader added in v1.0.4

func DoCheckHeader(h http.Handler, key Key) http.Handler

DoCheckHeader returns a handler that performs the CheckHeader logic

Types

type Key added in v1.0.4

type Key int

Key - iota enum of possible sets of keys for middleware manipulation

const (
	UserAccess Key = iota
	Locale
	CollectionID
)

Possible values for sets of keys

func (Key) Context added in v1.0.4

func (k Key) Context() request.ContextKey

Context returns the context key

func (Key) Cookie added in v1.0.4

func (k Key) Cookie() string

Cookie returns the cookie key

func (Key) Header added in v1.0.4

func (k Key) Header() string

Header returns the header key

type KeyMap added in v1.0.4

type KeyMap struct {
	Header  string
	Cookie  string
	Context request.ContextKey
}

KeyMap represents a mapping between header keys, cookie keys and context keys for an equivalent value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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