router

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2023 License: BSD-3-Clause Imports: 14 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMatchContext added in v1.5.1

func NewMatchContext(cxt context.Context, match *Match) context.Context

Types

type Attributes

type Attributes map[string]interface{}

Request attributes

func (Attributes) Copy added in v1.0.1

func (a Attributes) Copy() Attributes

type Context

type Context struct {
	Vars  path.Vars
	Attrs Attributes
	Path  string
}

Request context

type Handler

type Handler func(*Request, Context) (*Response, error)

Route handler

type Match added in v1.3.0

type Match struct {
	Method string
	Path   string
	Params url.Values
	Vars   path.Vars
}

A matched route

func MatchFromContext added in v1.5.0

func MatchFromContext(cxt context.Context) *Match

type Matcher added in v1.7.0

type Matcher func(*Request, Route) bool

Candidate route matcher

type Middleware

type Middleware interface {
	Wrap(Handler) Handler
}

Middleware provides functionality to wrap a handler producing another handler

type MiddlewareFunc added in v1.4.0

type MiddlewareFunc func(Handler) Handler

Middleware function wrapper

func (MiddlewareFunc) Wrap added in v1.4.0

func (m MiddlewareFunc) Wrap(h Handler) Handler

type Request

type Request http.Request

func NewRequest

func NewRequest(method, path string, entity io.Reader) (*Request, error)

func (*Request) Context added in v1.1.0

func (r *Request) Context() context.Context

func (*Request) OriginAddr added in v1.8.0

func (r *Request) OriginAddr() string

OriginAddr tries to identify the best address possible representing the origination of the request.

When available it prefers the `X-Forwarded-For` header, which is widely used by middleware infrastructure for this purpose. Failing that, the http.Request.RemotAddr is used with the port portion of the value removed.

If none of the above is available and empty string is returned.

type Response

type Response struct {
	Status int
	Header http.Header
	Entity io.ReadCloser
}

func NewResponse

func NewResponse(status int) *Response

func (*Response) ReadEntity

func (r *Response) ReadEntity() ([]byte, error)

func (*Response) SetBytes

func (r *Response) SetBytes(t string, d []byte) (*Response, error)

func (*Response) SetEntity

func (r *Response) SetEntity(e entity.Entity) (*Response, error)

func (*Response) SetHeader

func (r *Response) SetHeader(k, v string) *Response

func (*Response) SetJSON

func (r *Response) SetJSON(d interface{}) (*Response, error)

func (*Response) SetString

func (r *Response) SetString(t, d string) (*Response, error)

type Route

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

An individual route

func (*Route) Attr

func (r *Route) Attr(k string, v interface{}) *Route

Set an attribute

func (*Route) Context added in v1.6.0

func (r *Route) Context(match *Match) Context

Obtain a context for this route and the provided match

func (*Route) Describe added in v1.9.0

func (r *Route) Describe(verbose bool) string

Describe this route

func (*Route) Handle

func (r *Route) Handle(req *Request, cxt Context) (*Response, error)

Handle the request

func (*Route) Match added in v1.7.0

func (r *Route) Match(m Matcher) *Route

Match via a user-provided function

func (Route) Matches

func (r Route) Matches(req *Request, state *matchState) *Match

Matches the provided request or not; returns the details of the match if successful, otherwise nil.

func (*Route) Methods

func (r *Route) Methods(m ...string) *Route

Set methods

func (*Route) Param

func (r *Route) Param(k, v string) *Route

Match a single parameter

func (*Route) Params

func (r *Route) Params(p url.Values) *Route

Match a set of parameters

func (*Route) Paths

func (r *Route) Paths(s ...string) *Route

Add additional paths

func (*Route) String

func (r *Route) String() string

type Router

type Router interface {
	Use(m Middleware)
	Add(p string, f Handler) *Route
	Find(r *Request) (*Route, *Match, error)
	Handle(r *Request) (*Response, error)
	Subrouter(p string) Router
	Routes() []*Route
}

Dead simple router

func New

func New() Router

Directories

Path Synopsis
adapter

Jump to

Keyboard shortcuts

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