luci: Index | Examples | Files

package router

import ""

Package router provides an HTTP router with support for middleware and subrouters. It wraps around julienschmidt/httprouter.

Example_createServer demonstrates creating an HTTP server using the router package.


r := router.New()
r.GET("/hello", router.MiddlewareChain{}, func(c *router.Context) {
    fmt.Fprintf(c.Writer, "Hello")
r.GET("/hello/:name", router.MiddlewareChain{}, func(c *router.Context) {
    fmt.Fprintf(c.Writer, "Hello %s", c.Params.ByName("name"))

auth := r.Subrouter("authenticated")
auth.GET("/secret", router.NewMiddlewareChain(GenerateSecret), func(c *router.Context) {
    fmt.Fprintf(c.Writer, "secret: %d", c.Context.Value("secret"))

server := httptest.NewServer(r)
defer server.Close()



200 Hello
200 Hello darknessmyoldfriend
401 Authentication failed



Package Files

handler.go router.go

func RunMiddleware Uses

func RunMiddleware(c *Context, mc MiddlewareChain, h Handler)

RunMiddleware executes the middleware chain and handlers with the given initial context. Useful to execute a chain of functions in tests.

type Context Uses

type Context struct {
    Context     context.Context
    Writer      http.ResponseWriter
    Request     *http.Request
    Params      httprouter.Params
    HandlerPath string // the path with which the handler was registered

Context contains the context, response writer, request, and params shared across Middleware and Handler functions.

type Handler Uses

type Handler func(*Context)

Handler is the type for all request handlers.

type Middleware Uses

type Middleware func(c *Context, next Handler)

Middleware is a function that accepts a shared context and the next function. Since Middleware is typically part of a chain of functions that handles an HTTP request, it must obey the following rules.

- Middleware must call next if it has not written to the Context
  by the end of the function.
- Middleware must not call next if it has written to the Context.
- Middleware must not write to the Context after next is called and
  the Context has been written to.
- Middleware may modify the embedded context before calling next.

type MiddlewareChain Uses

type MiddlewareChain struct {
    // contains filtered or unexported fields

MiddlewareChain is an ordered collection of Middleware.

MiddlewareChain's zero value is a middleware chain with no Middleware. Allocating a MiddlewareChain with Middleware can be done with NewMiddlewareChain.

func NewMiddlewareChain Uses

func NewMiddlewareChain(mw ...Middleware) (mc MiddlewareChain)

NewMiddlewareChain creates a new MiddlewareChain with the supplied Middleware entries.

func (MiddlewareChain) Extend Uses

func (mc MiddlewareChain) Extend(mw ...Middleware) MiddlewareChain

Extend returns a new MiddlewareChain with the supplied Middleware appended to the end.

func (MiddlewareChain) ExtendFrom Uses

func (mc MiddlewareChain) ExtendFrom(other MiddlewareChain) MiddlewareChain

ExtendFrom returns a new MiddlewareChain with the supplied MiddlewareChain appended to the end.

type Router Uses

type Router struct {
    BasePath string
    // contains filtered or unexported fields

Router is the main type for the package. To create a Router, use New.

func New Uses

func New() *Router

New creates a Router.

func NewWithRootContext Uses

func NewWithRootContext(root context.Context) *Router

NewWithRootContext creates a router whose request contexts all inherit from the given context.

func (*Router) DELETE Uses

func (r *Router) DELETE(path string, mc MiddlewareChain, h Handler)

DELETE is a shortcut for router.Handle("DELETE", path, mc, h)

func (*Router) GET Uses

func (r *Router) GET(path string, mc MiddlewareChain, h Handler)

GET is a shortcut for router.Handle("GET", path, mc, h)

func (*Router) GetParams Uses

func (r *Router) GetParams(method, path string) (httprouter.Params, bool)

GetParams parases the httprouter.Params from the supplied method and path.

If nothing is registered for method/path, GetParams will return false.

func (*Router) HEAD Uses

func (r *Router) HEAD(path string, mc MiddlewareChain, h Handler)

HEAD is a shortcut for router.Handle("HEAD", path, mc, h)

func (*Router) Handle Uses

func (r *Router) Handle(method, path string, mc MiddlewareChain, h Handler)

Handle registers a middleware chain and a handler for the given method and path. len(mc)==0 is allowed. See for documentation on how the path may be formatted.

func (*Router) NotFound Uses

func (r *Router) NotFound(mc MiddlewareChain, h Handler)

NotFound sets the handler to be called when no matching route is found.

func (*Router) OPTIONS Uses

func (r *Router) OPTIONS(path string, mc MiddlewareChain, h Handler)

OPTIONS is a shortcut for router.Handle("OPTIONS", path, mc, h)

func (*Router) PATCH Uses

func (r *Router) PATCH(path string, mc MiddlewareChain, h Handler)

PATCH is a shortcut for router.Handle("PATCH", path, mc, h)

func (*Router) POST Uses

func (r *Router) POST(path string, mc MiddlewareChain, h Handler)

POST is a shortcut for router.Handle("POST", path, mc, h)

func (*Router) PUT Uses

func (r *Router) PUT(path string, mc MiddlewareChain, h Handler)

PUT is a shortcut for router.Handle("PUT", path, mc, h)

func (*Router) ServeHTTP Uses

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP makes Router implement the http.Handler interface.

func (*Router) Subrouter Uses

func (r *Router) Subrouter(relativePath string) *Router

Subrouter creates a new router with an updated base path. The new router copies middleware and configuration from the router it derives from.

func (*Router) Use Uses

func (r *Router) Use(mc MiddlewareChain)

Use adds middleware chains to the group. The added middleware applies to all handlers registered on the router and to all handlers registered on routers that may be derived from the router (using Subrouter).

Package router imports 4 packages (graph) and is imported by 112 packages. Updated 2020-10-20. Refresh now. Tools for package owners.