whmux

package
v0.0.0-...-3f30213 Latest Latest
Warning

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

Go to latest
Published: May 30, 2017 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package whmux provides some useful request mux helpers for demultiplexing requests to one of a number of handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exact

func Exact(h http.Handler) http.Handler

Exact is simply RequireGet and ExactPath called together.

func ExactPath

func ExactPath(h http.Handler) http.Handler

ExactPath takes an http.Handler that returns a new http.Handler that doesn't accept any more path elements and returns a 404 if more are provided.

func RequireGet

func RequireGet(h http.Handler) http.Handler

RequireGet is simply RequireMethod but called with "GET" as the first argument.

func RequireMethod

func RequireMethod(method string, h http.Handler) http.Handler

RequireMethod takes an http.Handler and returns a new http.Handler that only works with the given HTTP method. If a different method is used, a 405 is returned.

func Shift

func Shift(path string) (dir, left string)

Shift pulls the first directory out of the path and returns the remainder.

Types

type Dir

type Dir map[string]http.Handler

Dir is an http.Handler that mimics a directory. It mutates an incoming request's URL.Path to properly namespace handlers. This way a handler can assume it has the root of its section. If you want the original URL, use req.RequestURI (but don't modify it).

func (Dir) Routes

func (d Dir) Routes(
	cb func(method, path string, annotations map[string]string))

Routes implements whroute.Lister

func (Dir) ServeHTTP

func (d Dir) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.handler

type Host

type Host map[string]http.Handler

Host is an http.Handler that chooses a subhandler based on the request Host header. The star host ("*") is a default handler.

func (Host) Routes

func (h Host) Routes(
	cb func(method, path string, annotations map[string]string))

Routes implements whroute.Lister

func (Host) ServeHTTP

func (h Host) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.handler

type IntArg

type IntArg webhelp.ContextKey

IntArg is a way to pull off numeric path elements from an incoming URL. You'll need to create one with NewIntArg.

func NewIntArg

func NewIntArg() IntArg

func (IntArg) Get

func (a IntArg) Get(ctx context.Context) (val int64, ok bool)

Get returns a stored value for the Arg from the Context and ok = true if found, or ok = false if no value was found (which won't be the case if a higher-level handler was this arg)

func (IntArg) MustGet

func (a IntArg) MustGet(ctx context.Context) (val int64)

MustGet is like Get but panics in cases when ok would be false. If used with whfatal.Catch, will return a 404 to the user.

func (IntArg) Shift

func (a IntArg) Shift(h http.Handler) http.Handler

Shift takes an http.Handler and returns a new http.Handler that does additional request processing. When an incoming request is processed, the new http.Handler pulls the next path element off of the incoming request path and puts the value in the current Context. It then passes processing off to the wrapped http.Handler. It responds with a 404 if no numeric value is found.

func (IntArg) ShiftOpt

func (a IntArg) ShiftOpt(found, notfound http.Handler) http.Handler

ShiftOpt is like Shift but will only use the first handler if there's a numeric argument found and the second handler otherwise.

type Method

type Method map[string]http.Handler

Method is an http.Handler muxer that keys off of the given HTTP request method.

func (Method) Routes

func (m Method) Routes(
	cb func(method, path string, annotations map[string]string))

Routes implements whroute.Lister

func (Method) ServeHTTP

func (m Method) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.handler

type Overlay

type Overlay struct {
	Default http.Handler
	Overlay Dir
}

Overlay is essentially a Dir that you can put in front of another http.Handler. If the requested entry isn't in the overlay Dir, the Default will be used. If no Default is specified this works exactly the same as a Dir.

func (Overlay) Routes

func (o Overlay) Routes(
	cb func(method, path string, annotations map[string]string))

Routes implements whroute.Lister

func (Overlay) ServeHTTP

func (o Overlay) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.handler

type StringArg

type StringArg webhelp.ContextKey

StringArg is a way to pull off arbitrary path elements from an incoming URL. You'll need to create one with NewStringArg.

func NewStringArg

func NewStringArg() StringArg

func (StringArg) Get

func (a StringArg) Get(ctx context.Context) (val string)

Get returns a stored value for the Arg from the Context, or "" if no value was found (which won't be the case if a higher-level handler was this arg)

func (StringArg) Shift

func (a StringArg) Shift(h http.Handler) http.Handler

Shift takes an http.Handler and returns a new http.Handler that does additional request processing. When an incoming request is processed, the new http.Handler pulls the next path element off of the incoming request path and puts the value in the current Context. It then passes processing off to the wrapped http.Handler. The value will be an empty string if no argument is found.

func (StringArg) ShiftOpt

func (a StringArg) ShiftOpt(found, notfound http.Handler) http.Handler

ShiftOpt is like Shift but the first handler is used only if there's an argument found and the second handler is used if there isn't.

Jump to

Keyboard shortcuts

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