webhelp.v1: gopkg.in/webhelp.v1/whmux Index | Files

package whmux

import "gopkg.in/webhelp.v1/whmux"

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

Index

Package Files

argmux.go mux.go

func Exact Uses

func Exact(h http.Handler) http.Handler

Exact is simply RequireGet and ExactPath called together.

func ExactPath Uses

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 Uses

func RequireGet(h http.Handler) http.Handler

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

func RequireMethod Uses

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 Uses

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

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

type Dir Uses

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 Uses

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

Routes implements whroute.Lister

func (Dir) ServeHTTP Uses

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

ServeHTTP implements http.handler

type Host Uses

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 Uses

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

Routes implements whroute.Lister

func (Host) ServeHTTP Uses

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

ServeHTTP implements http.handler

type IntArg Uses

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 Uses

func NewIntArg() IntArg

func (IntArg) Get Uses

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 Uses

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 Uses

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 Uses

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 Uses

type Method map[string]http.Handler

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

func (Method) Routes Uses

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

Routes implements whroute.Lister

func (Method) ServeHTTP Uses

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

ServeHTTP implements http.handler

type Overlay Uses

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 Uses

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

Routes implements whroute.Lister

func (Overlay) ServeHTTP Uses

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

ServeHTTP implements http.handler

type StringArg Uses

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 Uses

func NewStringArg() StringArg

func (StringArg) Get Uses

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 Uses

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 Uses

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.

Package whmux imports 9 packages (graph) and is imported by 5 packages. Updated 2017-06-05. Refresh now. Tools for package owners.