runtime: Index | Files

package denco

import ""

Package denco provides fast URL router.


Package Files

router.go server.go util.go


const (
    // ParamCharacter is a special character for path parameter.
    ParamCharacter = ':'

    // WildcardCharacter is a special character for wildcard path parameter.
    WildcardCharacter = '*'

    // TerminationCharacter is a special character for end of path.
    TerminationCharacter = '#'

    // SeparatorCharacter separates path segments.
    SeparatorCharacter = '/'

    // MaxSize is max size of records and internal slice.
    MaxSize = (1 << 22) - 1


var NotFound = func(w http.ResponseWriter, r *http.Request, _ Params) {
    http.NotFound(w, r)

NotFound replies to the request with an HTTP 404 not found error. NotFound is called when unknown HTTP method or a handler not found. If you want to use the your own NotFound handler, please overwrite this variable.

func NextSeparator Uses

func NextSeparator(path string, start int) int

NextSeparator returns an index of next separator in path.

type Handler Uses

type Handler struct {
    // Method is an HTTP method.
    Method string

    // Path is a routing path for handler.
    Path string

    // Func is a function of handler of HTTP request.
    Func HandlerFunc

Handler represents a handler of HTTP request.

type HandlerFunc Uses

type HandlerFunc func(w http.ResponseWriter, r *http.Request, params Params)

The HandlerFunc type is aliased to type of handler function.

type Mux Uses

type Mux struct{}

Mux represents a multiplexer for HTTP request.

func NewMux Uses

func NewMux() *Mux

NewMux returns a new Mux.

func (*Mux) Build Uses

func (m *Mux) Build(handlers []Handler) (http.Handler, error)

Build builds a http.Handler.

func (*Mux) GET Uses

func (m *Mux) GET(path string, handler HandlerFunc) Handler

GET is shorthand of Mux.Handler("GET", path, handler).

func (*Mux) HEAD Uses

func (m *Mux) HEAD(path string, handler HandlerFunc) Handler

HEAD is shorthand of Mux.Handler("HEAD", path, handler).

func (*Mux) Handler Uses

func (m *Mux) Handler(method, path string, handler HandlerFunc) Handler

Handler returns a handler for HTTP method.

func (*Mux) POST Uses

func (m *Mux) POST(path string, handler HandlerFunc) Handler

POST is shorthand of Mux.Handler("POST", path, handler).

func (*Mux) PUT Uses

func (m *Mux) PUT(path string, handler HandlerFunc) Handler

PUT is shorthand of Mux.Handler("PUT", path, handler).

type Param Uses

type Param struct {
    Name  string
    Value string

Param represents name and value of path parameter.

type Params Uses

type Params []Param

Params represents the name and value of path parameters.

func (Params) Get Uses

func (ps Params) Get(name string) string

Get gets the first value associated with the given name. If there are no values associated with the key, Get returns "".

type Record Uses

type Record struct {
    // Key for router construction.
    Key string

    // Result value for Key.
    Value interface{}

Record represents a record data for router construction.

func NewRecord Uses

func NewRecord(key string, value interface{}) Record

NewRecord returns a new Record.

type Router Uses

type Router struct {
    // SizeHint expects the maximum number of path parameters in records to Build.
    // SizeHint will be used to determine the capacity of the memory to allocate.
    // By default, SizeHint will be determined from given records to Build.
    SizeHint int
    // contains filtered or unexported fields

Router represents a URL router.

func New Uses

func New() *Router

New returns a new Router.

func (*Router) Build Uses

func (rt *Router) Build(records []Record) error

Build builds URL router from records.

func (*Router) Lookup Uses

func (rt *Router) Lookup(path string) (data interface{}, params Params, found bool)

Lookup returns data and path parameters that associated with path. params is a slice of the Param that arranged in the order in which parameters appeared. e.g. when built routing path is "/path/to/:id/:name" and given path is "/path/to/1/alice". params order is [{"id": "1"}, {"name": "alice"}], not [{"name": "alice"}, {"id": "1"}].

Package denco imports 4 packages (graph) and is imported by 12 packages. Updated 2020-02-16. Refresh now. Tools for package owners.