router

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2024 License: BSD-3-Clause Imports: 14 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMatchContext

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

Types

type Attributes

type Attributes map[string]interface{}

Request attributes

func (Attributes) Copy

func (a Attributes) Copy() Attributes

func (Attributes) String

func (a Attributes) String() string

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

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

A matched route

func MatchFromContext

func MatchFromContext(cxt context.Context) *Match

type Matcher

type Matcher func(*Request, Route) bool

Candidate route matcher

type Middle

type Middle interface {
	Wrap(Handler) Handler
}

Middle provides functionality to wrap a handler producing another handler

type MiddleFunc

type MiddleFunc func(Handler) Handler

Middle function wrapper

func (MiddleFunc) Wrap

func (m MiddleFunc) Wrap(h Handler) Handler

MiddleFunc conforms to Middle by calling itself with the handler

type Middles

type Middles []Middle

A set of middleware

func (Middles) Wrap

func (m Middles) Wrap(h Handler) Handler

Middles is itself conforms to Middle and wraps a handler with all of its sub-middleware

type Request

type Request http.Request

func NewRequest

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

func (*Request) Context

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

func (*Request) OriginAddr

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

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

Obtain a context for this route and the provided match

func (*Route) Describe

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

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

Method sets the methods matched by a route

func (*Route) Param

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

Param matches a single parameter

func (*Route) Params

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

Params matches a set of parameter

func (*Route) Paths

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

Paths sets the paths matched by a route

func (*Route) String

func (r *Route) String() string

func (*Route) Use

func (r *Route) Use(m ...Middle) *Route

Use applies middleware to a route. Route-level middleware is applied in the order it is declared AFTER router-level middleware.

type Router

type Router interface {
	Use(m Middle)
	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

Jump to

Keyboard shortcuts

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