mux: github.com/fragmenta/mux Index | Files | Directories

package mux

import "github.com/fragmenta/mux"

Index

Package Files

handlers.go mux.go params.go route.go

Variables

var MaxCacheEntries = 500

MaxCacheEntries defines the maximum number of entries in the request->route cache 0 means caching is turned off

func SetDefault Uses

func SetDefault(m *Mux)

SetDefault sets the default mux on the package for use in parsing params we could instead decorate each request with a reference to the Route but this means extra allocations for each request, when almost all apps require only one mux.

type ErrorHandlerFunc Uses

type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)

ErrorHandlerFunc defines a HandlerFunc which accepts an error and displays it.

type HandlerFunc Uses

type HandlerFunc func(w http.ResponseWriter, r *http.Request) error

HandlerFunc defines a std net/http HandlerFunc, but which returns an error.

type Middleware Uses

type Middleware func(http.HandlerFunc) http.HandlerFunc

Middleware is a handler that wraps another handler

type Mux Uses

type Mux struct {

    // See httptrace for best way to instrument
    ErrorHandler ErrorHandlerFunc
    FileHandler  HandlerFunc
    RedirectWWW  bool
    // contains filtered or unexported fields
}

Mux handles http requests by selecting a handler and passing the request to it. Routes are evaluated in the order they were added. Before the request reaches the handler it is passed through the middleware chain.

func New Uses

func New() *Mux

New returns a new mux

func (*Mux) Add Uses

func (m *Mux) Add(pattern string, handler HandlerFunc) Route

Add adds a route for this request with the default methods (GET/HEAD) Route is returned so that method functions can be chained

func (*Mux) AddHandler Uses

func (m *Mux) AddHandler(pattern string, handler http.HandlerFunc) Route

AddHandler adds a route for this pattern using a stdlib http.HandlerFunc which does not return an error.

func (*Mux) AddMiddleware Uses

func (m *Mux) AddMiddleware(middleware Middleware)

AddMiddleware adds a middleware function, this should be done before starting the server as it remakes our chain of middleware. This prepends to our chain of middleware

func (*Mux) Get Uses

func (m *Mux) Get(pattern string, handler HandlerFunc) Route

Get adds a route for this pattern/hanlder with the default methods (GET/HEAD)

func (*Mux) Match Uses

func (m *Mux) Match(r *http.Request) Route

Match finds the route (if any) which matches this request

func (*Mux) Post Uses

func (m *Mux) Post(pattern string, handler HandlerFunc) Route

Post adds a route for this pattern/hanlder with method http.PostMethod

func (*Mux) RouteRequest Uses

func (m *Mux) RouteRequest(w http.ResponseWriter, r *http.Request)

RouteRequest is the final endpoint of all requests

func (*Mux) ServeHTTP Uses

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

ServeHTTP implements net/http.Handler.

type NaiveRoute Uses

type NaiveRoute struct {
    // contains filtered or unexported fields
}

NaiveRoute holds a pattern which matches a route and params within it, and an associated handler which will be called when the route matches.

func (*NaiveRoute) Delete Uses

func (r *NaiveRoute) Delete() Route

Delete sets the method exclusively to DELETE

func (*NaiveRoute) Get Uses

func (r *NaiveRoute) Get() Route

Get sets the method exclusively to GET

func (*NaiveRoute) Handle Uses

func (r *NaiveRoute) Handle(w http.ResponseWriter, req *http.Request) error

Handle calls the handler with the writer and request.

func (*NaiveRoute) Handler Uses

func (r *NaiveRoute) Handler() HandlerFunc

Handler returns our handlerfunc.

func (*NaiveRoute) Match Uses

func (r *NaiveRoute) Match(path string) bool

Match returns true if this route matches the path given.

func (*NaiveRoute) MatchMaybe Uses

func (r *NaiveRoute) MatchMaybe(path string) bool

MatchMaybe returns false if the path definitely is not MatchMethod or true/maybe if it *may* match.

func (*NaiveRoute) MatchMethod Uses

func (r *NaiveRoute) MatchMethod(method string) bool

MatchMethod returns true if our list of methods contains method

func (*NaiveRoute) Method Uses

func (r *NaiveRoute) Method(method string) Route

Method sets the method exclusively to method

func (*NaiveRoute) Methods Uses

func (r *NaiveRoute) Methods(permitted ...string) Route

Methods sets the methods allowed as an array

func (*NaiveRoute) Parse Uses

func (r *NaiveRoute) Parse(path string) map[string]string

Parse parses this path given our regexp and returns a map of URL params.

func (*NaiveRoute) Pattern Uses

func (r *NaiveRoute) Pattern() string

Pattern returns the string pattern for the route

func (*NaiveRoute) Post Uses

func (r *NaiveRoute) Post() Route

Post sets the method exclusively to POST

func (*NaiveRoute) Put Uses

func (r *NaiveRoute) Put() Route

Put sets the method exclusively to PUT

func (*NaiveRoute) Setup Uses

func (r *NaiveRoute) Setup(p string, h HandlerFunc) error

Setup sets up the route from a pattern

func (*NaiveRoute) String Uses

func (r *NaiveRoute) String() string

String returns the route formatted as a string

type PrefixRoute Uses

type PrefixRoute struct {
    NaiveRoute
    // contains filtered or unexported fields
}

PrefixRoute uses a static prefix to reject route matches quickly.

func (*PrefixRoute) MatchMaybe Uses

func (r *PrefixRoute) MatchMaybe(path string) bool

MatchMaybe returns false if the path definitely is not MatchMethod or true/maybe if it *may* match.

func (*PrefixRoute) Setup Uses

func (r *PrefixRoute) Setup(p string, h HandlerFunc) error

Setup sets up the pattern prefix for the Prefix route.

func (*PrefixRoute) String Uses

func (r *PrefixRoute) String() string

String returns the route formatted as a string.

type RequestParams Uses

type RequestParams struct {
    Values url.Values
    Files  map[string][]*multipart.FileHeader
}

RequestParams parses all params in a request and stores them in Values this includes: path params (from route) query params (from request) body params (from form request bodies)

func Params Uses

func Params(r *http.Request) (*RequestParams, error)

Params returns a new set of params parsed from the request.

func ParamsWithMux Uses

func ParamsWithMux(m *Mux, r *http.Request) (*RequestParams, error)

ParamsWithMux returns params for a given mux and request

func (*RequestParams) Add Uses

func (p *RequestParams) Add(key string, values []string)

Add appends these values to this key, without removing any other entries.

func (*RequestParams) Delete Uses

func (p *RequestParams) Delete(key string)

Delete all values associated with the key.

func (*RequestParams) Exists Uses

func (p *RequestParams) Exists(key string) bool

Exists returns true if this key exists in Values

func (*RequestParams) Get Uses

func (p *RequestParams) Get(key string) string

Get returns the first value for this key or a blank string if no entry.

func (*RequestParams) GetDate Uses

func (p *RequestParams) GetDate(key string, format string) (time.Time, error)

GetDate returns the first value associated with a given key as a time,

using the given time format.

func (*RequestParams) GetFloat Uses

func (p *RequestParams) GetFloat(key string) float64

GetFloat returns the first value associated with the key as an integer. If there is no value or a parse error, it returns 0.0

func (*RequestParams) GetFloats Uses

func (p *RequestParams) GetFloats(key string) []float64

GetFloats returns all values associated with the key as an array of floats.

func (*RequestParams) GetInt Uses

func (p *RequestParams) GetInt(key string) int64

GetInt returns the first value associated with the given key as an integer. If there is no value or a parse error, it returns 0 If the string contains non-numeric characters, it is truncated from the first non-numeric character.

func (*RequestParams) GetInts Uses

func (p *RequestParams) GetInts(key string) []int64

GetInts returns all values associated with the key as an array of integers.

func (*RequestParams) GetIntsString Uses

func (p *RequestParams) GetIntsString(key string) string

GetIntsString returns all values associated with the key as a comma separated string.

func (*RequestParams) GetStrings Uses

func (p *RequestParams) GetStrings(key string) []string

GetStrings returns all string values associated with this key if there are no values associated an empty array is returned

func (*RequestParams) GetUniqueInts Uses

func (p *RequestParams) GetUniqueInts(key string) []int64

GetUniqueInts returns all unique non-zero int values associated with the given key as an array of integers

func (*RequestParams) Map Uses

func (p *RequestParams) Map() map[string]string

Map returns a flattened map of params with only one entry for each key, rather than the array of values Request params allow.

func (*RequestParams) Set Uses

func (p *RequestParams) Set(key string, values []string)

Set sets this key to these values, removing any other entries.

func (*RequestParams) SetInt Uses

func (p *RequestParams) SetInt(key string, v int64)

SetInt sets this key to this single string value, removing any other entries.

func (*RequestParams) SetString Uses

func (p *RequestParams) SetString(key string, v string)

SetString sets this key to this single string value, removing any other entries.

type Route Uses

type Route interface {
    // Match against URL
    MatchMethod(string) bool
    MatchMaybe(string) bool
    Match(string) bool

    // Handler returns the handler to execute
    Handler() HandlerFunc

    // Parse the URL for params according to pattern
    Parse(string) map[string]string

    // Set accepted methods
    Get() Route
    Post() Route
    Put() Route
    Delete() Route
    Methods(...string) Route
}

Route defines the interface routes are expected to conform to.

func NewNaiveRoute Uses

func NewNaiveRoute(pattern string, handler HandlerFunc) (Route, error)

NewNaiveRoute creates a new Route, given a pattern to match and a handler for the route.

func NewPrefixRoute Uses

func NewPrefixRoute(pattern string, handler HandlerFunc) (Route, error)

NewPrefixRoute creates a new PrefixRoute, given a pattern to match and a handler for the route.

func NewRoute Uses

func NewRoute(pattern string, handler HandlerFunc) (Route, error)

NewRoute returns a new Route of our default type.

Directories

PathSynopsis
logPackage log provides logging interfaces for use in handlers and loggers for stdout, files, and time series databases
middleware/gzipPackage gzip provides gzip middleware to gzip responses where appropriate
middleware/logrequest
middleware/securePackage secure adds headers to protect against xss and reflection attacks and force use of https

Package mux imports 12 packages (graph) and is imported by 13 packages. Updated 2018-10-05. Refresh now. Tools for package owners.