xrouter

package module
v2.0.6+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2017 License: Apache-2.0 Imports: 10 Imported by: 0

README

xrouter

A router which combines httprouter and alice for middleware.

Documentation

Index

Constants

View Source
const ParamsKey = "params"

ParamsKey is the key for contexts which grant access to the url params.

Variables

This section is empty.

Functions

func HTTPHandler

func HTTPHandler(c alice.Chain, fs http.Handler) http.Handler

HTTPHandler wraps a raw http.Handler in chain middleware.

func LogHandler

func LogHandler() func(http.Handler) http.Handler

LogHandler instantiates a new xlog HTTP handler using the given log.

func Param

func Param(ctx context.Context, key string) string

Param returns a URL parameter by name

Types

type AddHandlerEvent

type AddHandlerEvent struct {
	Method string
	Path   string
}

AddHandlerEvent is fired whenever a generic handler is added to the router.

type Event

type Event interface{}

Event represents router events which will fire as the router is being configured. This is used more as a debug tool than anything.

type MethodNotAllowedHandlerEvent

type MethodNotAllowedHandlerEvent struct {
}

MethodNotAllowedHandlerEvent is fired when a MethodNotAllowed handler is set for the router.

type NotFoundHandlerEvent

type NotFoundHandlerEvent struct {
}

NotFoundHandlerEvent is fired when a NotFound handler is set for the router.

type Route

Route is a function with exposes the request context as an argument. For Go 1.7+, the request has an attached context. This provides for backward compatibility for the many handlers written before the Go 1.7.

type Router

type Router interface {
	RouterGroup

	// Static adds a directory of static content to serve at root.
	StaticRoot(fs http.Handler)

	// StaticFiles adds a directory of static content to a specific path.
	StaticFiles(path string, fs http.Handler)

	// NotFound adds a handler for routes that don't exist.
	NotFound(http.Handler)

	// MethodNotAllowed handles requests in which the route exists but hte wrong method was used.
	MethodNotAllowed(http.Handler)

	// Handler returns an http.Handler
	Handler() http.Handler

	// EventHandler calls the given function for each handler as it is added to the router.
	EventHandler(func(evt Event))
}

Router defines a root router for handling requests.

func New

func New() Router

New creates a router which wraps an httprouter.

type RouterGroup

type RouterGroup interface {

	// Use adds middleware to the router.
	Use(f func(next http.Handler) http.Handler)

	// Chain returns the middleware chain.
	Chain() alice.Chain

	// Group returns a new router which strips the given path before the request is handled. All middleware is transferred to the child group.
	Group(path string) RouterGroup

	// Path returns the root path of the RouterGroup
	Path() string

	// GET adds a GET handler at the given path.
	GET(path string, handler Route)

	// POST adds a POST handler at the given path.
	POST(path string, handler Route)

	// PUT adds a PUT handler at the given path.
	PUT(path string, handler Route)

	// OPTIONS adds a OPTIONS handler at the given path.
	OPTIONS(path string, handler Route)

	// HEAD adds a HEAD handler at the given path.
	HEAD(path string, handler Route)

	// PATCH adds a PATCH handler at the given path.
	PATCH(path string, handler Route)

	// DELETE adds a DELETE handler at the given path.
	DELETE(path string, handler Route)
}

RouterGroup allows for grouping routes with separate middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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