Rafał Lorenz edited this page Jun 1, 2017 · 8 revisions

Package gorouter provides request router with middleware

import "github.com/vardius/gorouter"

Index

Constants

HTTP methods constants

const (
    GET     = "GET"
    POST    = "POST"
    PUT     = "PUT"
    DELETE  = "DELETE"
    PATCH   = "PATCH"
    OPTIONS = "OPTIONS"
    HEAD    = "HEAD"
    CONNECT = "CONNECT"
    TRACE   = "TRACE"
)

type MiddlewareFunc

MiddlewareFunc is a middleware function type. It accepts as a parameter http.Handler and returns http.Handler as well Long story - short: it is a http.Handler wrapper

type MiddlewareFunc func(http.Handler) http.Handler

type Param

Param object to hold request parameter

type Param struct {
    Key   string
    Value string
}

type Params

Params slice returned from request context

type Params []Param

func FromContext

FromContext extracts the request Params ctx, if present.

func FromContext(ctx context.Context) (Params, bool)

func (Params) Value

Value of the request parameter by name

func (p Params) Value(key string) string

type Router

Router is a micro framwework, HTTP request router, multiplexer, mux

type Router interface {
    //Handle adds http.Handler as router handler
    //under given method and patter
    Handle(method, pattern string, handler http.Handler)

    //HandleFunc adds http.HandlerFunc as router handler
    //under given method and patter
    HandleFunc(method, pattern string, handler http.HandlerFunc)

    //POST adds http.Handler as router handler
    //under POST method and given patter
    POST(pattern string, handler http.Handler)

    //GET adds http.Handler as router handler
    //under GET method and given patter
    GET(pattern string, handler http.Handler)

    //PUT adds http.Handler as router handler
    //under PUT method and given patter
    PUT(pattern string, handler http.Handler)

    //DELETE adds http.Handler as router handler
    //under DELETE method and given patter
    DELETE(pattern string, handler http.Handler)

    //PATCH adds http.Handler as router handler
    //under PATCH method and given patter
    PATCH(pattern string, handler http.Handler)

    //OPTIONS adds http.Handler as router handler
    //under OPTIONS method and given patter
    OPTIONS(pattern string, handler http.Handler)

    //HEAD adds http.Handler as router handler
    //under HEAD method and given patter
    HEAD(pattern string, handler http.Handler)

    //CONNECT adds http.Handler as router handler
    //under CONNECT method and given patter
    CONNECT(pattern string, handler http.Handler)

    //TRACE adds http.Handler as router handler
    //under TRACE method and given patter
    TRACE(pattern string, handler http.Handler)

    //USE adds middleware functions ([]MiddlewareFunc)
    //to whole router branch under given method and patter
    USE(method, pattern string, fs ...MiddlewareFunc)

    //ServeHTTP dispatches the request to the route handler
    //whose pattern matches the request URL
    ServeHTTP(http.ResponseWriter, *http.Request)

    //ServeFile replies to the request with the
    //contents of the named file or directory.
    ServeFiles(path string, strip bool)

    //NotFound replies to the request with the
    //404 Error code
    NotFound(http.Handler)

    //NotFound replies to the request with the
    //405 Error code
    NotAllowed(http.Handler)
}

func New

New creates new Router instance, return pointer

func New(fs ...MiddlewareFunc) Router
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.