router

package module
v0.0.0-...-b1320ef Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrGenerateParameters is returned when generating a route with wrong parameters.
	ErrGenerateParameters = errors.New("params contains wrong parameters")

	// ErrNotFoundRoute is returned when generating a route that can not find route in tree.
	ErrNotFoundRoute = errors.New("cannot find route in tree")

	// ErrNotFoundMethod is returned when generating a route that can not find method in tree.
	ErrNotFoundMethod = errors.New("cannot find method in tree")

	// ErrPatternGrammar is returned when generating a route that pattern grammar error.
	ErrPatternGrammar = errors.New("pattern grammar error")
)

Functions

func GetParam

func GetParam(r *http.Request, key string) string

GetParam returns route param stored in http.request.

Types

type Context

type Context interface {
	ContextInput() InputContext
	parameters.Parameters
}

Context of router

type HandlerFunc

type HandlerFunc func(Context)

HandlerFunc of router

type InputContext

type InputContext interface {
	Path() string
	Method() string
}

InputContext of router

type MiddlewareType

type MiddlewareType func(next HandlerFunc) HandlerFunc

MiddlewareType is a public type that is used for middleware

type Node

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

Node records any URL params, and executes an end handler.

func NewNode

func NewNode(key string, depth int) *Node

NewNode returns a newly initialized Node object that implements the Node

type Parameters

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

Parameters records some parameters

type ParamsMapType

type ParamsMapType map[string]string

ParamsMapType is a private type that is used to store route params

func GetAllParams

func GetAllParams(r *http.Request) ParamsMapType

GetAllParams returns all route params stored in http.Request.

type Router

type Router struct {

	// PanicHandler for handling panic.
	PanicHandler func(ctx Context, err interface{})
	// contains filtered or unexported fields
}

Router is a simple HTTP route multiplexer that parses a request path, records any URL params, and executes an end handler.

func New

func New() *Router

New returns a newly initialized Router object that implements the Router

func (*Router) Generate

func (r *Router) Generate(method string, routeName string, params map[string]string) (string, error)

Generate returns reverse routing by method, routeName and params

func (*Router) Group

func (r *Router) Group(prefix string) *Router

Group define routes groups if there is a path prefix that uses `prefix`

func (*Router) Handle

func (r *Router) Handle(method string, path string, handle HandlerFunc)

Handle registers a new request handler with the given path and method.

func (*Router) Match

func (r *Router) Match(requestURL string, path string) bool

Match checks if the request matches the route pattern

func (*Router) NotFoundFunc

func (r *Router) NotFoundFunc(handler http.HandlerFunc)

NotFoundFunc registers a handler when the request route is not found

func (*Router) Serve

func (r *Router) Serve(inputContext InputContext) bool

Serve makes the router implement the router.Context interface.

func (*Router) Use

func (r *Router) Use(middleware ...MiddlewareType)

Use appends a middleware handler to the middleware stack.

type Tree

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

Tree records node

func NewTree

func NewTree() *Tree

NewTree returns a newly initialized Tree object that implements the Tree

func (*Tree) Add

func (t *Tree) Add(pattern string, handle HandlerFunc, middleware ...MiddlewareType)

Add use `pattern` 、handle、middleware stack as node register to tree

func (*Tree) Find

func (t *Tree) Find(pattern string, isRegex bool) (nodes []*Node)

Find returns nodes that the request match the route pattern

Jump to

Keyboard shortcuts

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