/turnpike

A trie-based HTTP multiplexer for Go with support for regex matching and middleware.

Primary LanguageGoMIT LicenseMIT

Turnpike

A trie-based HTTP multiplexer for Go with support for regex matching and middleware.

Usage

const (
	PathRoot              = "/"
	PathDelimiter         = PathRoot
	ParameterDelimiter    = ":"
	PatternDelimiterStart = "["
	PatternDelimiterEnd   = "]"
	PatternWildcard       = "(.+)"
)
var (
	ErrNotFound         = errors.New("no matching route record found")
	ErrMethodNotAllowed = errors.New("method not allowed")
)
var (
	DefaultNotFoundHandler         = http.NotFoundHandler
	DefaultMethodNotAllowedHandler = func() http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusMethodNotAllowed)
		})
	}
)

func GetParam

func GetParam(ctx context.Context, key string) string

GetParam retrieves from context a value corresponding to a given key.

type Route

type Route struct {
}

Route represents a route record to be used by a Router.

type Router

type Router struct {
	NotFoundHandler         http.Handler
	MethodNotAllowedHandler http.Handler
}

Router represents a multiplexer that routes HTTP requests.

func NewRouter

func NewRouter() *Router

NewRouter constructs and returns a pointer to a new Router.

func (*Router) FileHandler

func (r *Router) FileHandler(path string, root http.FileSystem) *Router

FileHandler registers a route handler as a file server, serving the directory qualified as path. This method effectively replaces the Handler() stage of the route pipeline. @todo allow usage of custom NotFoundHandler

func (*Router) Handler

func (r *Router) Handler(path string, handler http.Handler) *Router

Handler adds a path and handler to the current Route record.

func (*Router) Register

func (r *Router) Register()

Register registers the current Route record. This method must be invoked to register the Route.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP routes an HTTP request to the appropriate Route record handler.

func (*Router) Use

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

Use adds middlewares to the current Route record.

func (*Router) WithMethods

func (r *Router) WithMethods(methods ...string) *Router

WithMethods appends user-specified HTTP methods to the current Route record.