cobweb

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

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

Go to latest
Published: Oct 8, 2019 License: Apache-2.0 Imports: 8 Imported by: 2

README

A high performance HTTP request router

Documentation

Overview

Package denco provides fast URL router.

Index

Constants

View Source
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 = '#'

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

Variables

Functions

func CleanPath

func CleanPath(p string) string

func NextSeparator

func NextSeparator(path string, start int) int

NextSeparator returns an index of next separator in path.

Types

type Mux

type Mux struct {
	NotFound http.HandlerFunc
	// contains filtered or unexported fields
}

func New

func New() *Mux

func (*Mux) Any

func (m *Mux) Any(pattern string, handlers ...http.HandlerFunc)

func (*Mux) Build

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

func (*Mux) Delete

func (m *Mux) Delete(pattern string, handlers ...http.HandlerFunc)

func (*Mux) Get

func (m *Mux) Get(pattern string, handlers ...http.HandlerFunc)

func (*Mux) Group

func (m *Mux) Group(pattern string, fn func(), handlers ...http.HandlerFunc)

func (*Mux) Head

func (m *Mux) Head(pattern string, handlers ...http.HandlerFunc)

func (*Mux) Options

func (m *Mux) Options(pattern string, handlers ...http.HandlerFunc)

func (*Mux) Patch

func (m *Mux) Patch(pattern string, handlers ...http.HandlerFunc)

func (*Mux) Post

func (m *Mux) Post(pattern string, handlers ...http.HandlerFunc)

func (*Mux) Put

func (m *Mux) Put(pattern string, handlers ...http.HandlerFunc)

type Param

type Param struct {
	Name  string
	Value string
}

Param represents name and value of path parameter.

type Params

type Params []Param

Params represents the name and value of path parameters.

func (Params) Get

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

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

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

NewRecord returns a new Record.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// Status returns the status code of the response or 200 if the response has
	// not been written (as this is the default response code in net/http)
	Status() int
	// Written returns whether or not the ResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
	// Before allows for a function to be called before the ResponseWriter has been written to. This is
	// useful for setting headers or any other operations that must happen before a response has been written.
	Before(func(ResponseWriter))
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) ResponseWriter

NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter

type Router

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 NewRouter

func NewRouter() *Router

New returns a new Router.

func (*Router) Build

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

Build builds URL router from records.

func (*Router) Lookup

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"}].

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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