mux

package module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2023 License: GPL-2.0 Imports: 7 Imported by: 3

README

A multiplexer for Go

Supports
  • Variables in the path
  • The stdlib http.Handler interface (Also webassembly!)
  • stdlib http.ResponseWriter/http.Request functions
  • Route namespaces

For examples; please do not hesitate to look at the router_tests.go file.

Documentation

Index

Constants

View Source
const (
	// A special method, which will not be returned by any request, but can be set on the route to allow any method to pass.
	ANY     = "*"
	GET     = "GET"     // Equivalent to http.MethodGet
	POST    = "POST"    // Equivalent to http.MethodPost
	PUT     = "PUT"     // Equivalent to http.MethodPut
	DELETE  = "DELETE"  // Equivalent to http.MethodDelete
	HEAD    = "HEAD"    // Equivalent to http.MethodHead
	PATCH   = "PATCH"   // Equivalent to http.MethodPatch
	OPTIONS = "OPTIONS" // Equivalent to http.MethodOptions
)

Variables

View Source
var (
	VARIABLE_DELIMS = []string{"<<", ">>"}
	URL_DELIM       = "/"
	GLOB            = "*"
)

Global variables for use in the package.

These are exported so that they can be changed if needed.

Functions

func GetHost

func GetHost(r *http.Request) string

func GetIP

func GetIP(r *http.Request, proxied bool) string

func SetVariables

func SetVariables(r *http.Request, v Variables) *http.Request

func SplitPath

func SplitPath(path string) []string

SplitPath splits a path into its parts.

Types

type HandleFunc

type HandleFunc func(w http.ResponseWriter, req *http.Request)

type Handler

type Handler http.Handler

func NewHandler

func NewHandler(f HandleFunc) Handler

type Middleware

type Middleware func(next Handler) Handler

Middleware which will run before/after the HandleFunc.

type Mux

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

The muxer.

func New

func New() *Mux

func (*Mux) Any

func (r *Mux) Any(path string, handler Handler, name ...string) *Route

func (*Mux) Delete

func (r *Mux) Delete(path string, handler Handler, name ...string) *Route

func (*Mux) Find

func (r *Mux) Find(name string) *Route

func (*Mux) Get

func (r *Mux) Get(path string, handler Handler, name ...string) *Route

func (*Mux) Handle

func (r *Mux) Handle(method string, path string, handler Handler, name ...string) *Route

func (*Mux) HandleFunc added in v1.2.0

func (r *Mux) HandleFunc(method string, path string, handler http.HandlerFunc, name ...string) *Route

func (*Mux) Head

func (r *Mux) Head(path string, handler Handler, name ...string) *Route

func (*Mux) Match

func (r *Mux) Match(method string, path string) (*Route, Variables)

func (*Mux) NotFound

func (r *Mux) NotFound(w http.ResponseWriter, req *http.Request)

func (*Mux) Options

func (r *Mux) Options(path string, handler Handler, name ...string) *Route

func (*Mux) Patch

func (r *Mux) Patch(path string, handler Handler, name ...string) *Route

func (*Mux) Post

func (r *Mux) Post(path string, handler Handler, name ...string) *Route

func (*Mux) Put

func (r *Mux) Put(path string, handler Handler, name ...string) *Route

func (*Mux) RemoveByPath added in v1.0.4

func (r *Mux) RemoveByPath(path string)

func (*Mux) RemoveRoute added in v1.0.4

func (r *Mux) RemoveRoute(route *Route)

func (*Mux) ResetRoutes added in v1.0.5

func (r *Mux) ResetRoutes()

func (*Mux) ServeHTTP

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

func (*Mux) Use

func (r *Mux) Use(middleware ...Middleware)

type PathInfo

type PathInfo struct {
	IsGlob bool
	Path   []*PathPart
}

PathInfo contains information about a path.

It can be used to match a path, and retrieve variables from it.

func NewPathInfo

func NewPathInfo(path string) *PathInfo

NewPathInfo creates a new PathInfo object from a path string.

The path string can contain variables, which are defined by the text between the VARIABLE_DELIMS.

This function will panic if the GLOB is not the last part of the path.

func (*PathInfo) CopyAppend

func (p *PathInfo) CopyAppend(other *PathInfo) *PathInfo

Copies the slice, and append appends the other path to the end of this path.

It will panic if the path on which this was called is a glob.

func (*PathInfo) Match

func (p *PathInfo) Match(path []string) (bool, Variables)

Match matches a path to this path.

It returns whether the path matched, and the variables in the path.

If the path does not match, the variables will be nil.

func (*PathInfo) String

func (p *PathInfo) String() string

String returns a string representation of the path.

type PathPart

type PathPart struct {
	Part       string
	IsVariable bool
	IsGlob     bool
}

PathPart contains information about a part of a path.

type Route

type Route struct {
	Name       string
	Method     string
	Middleware []Middleware
	Path       *PathInfo
	Children   []*Route
	Handler    Handler
	Parent     *Route
	ParentMux  *Mux
	// contains filtered or unexported fields
}

func (*Route) Any

func (r *Route) Any(path string, handler Handler, name ...string) *Route

func (*Route) Delete

func (r *Route) Delete(path string, handler Handler, name ...string) *Route

func (*Route) Find

func (r *Route) Find(names []string) (*Route, bool)

func (*Route) Get

func (r *Route) Get(path string, handler Handler, name ...string) *Route

func (*Route) Handle

func (r *Route) Handle(method string, path string, handler Handler, name ...string) *Route

Handle adds a handler to the route.

It returns the route that was added so that it can be used to add children.

func (*Route) HandleFunc

func (r *Route) HandleFunc(method string, path string, handler func(w http.ResponseWriter, r *http.Request), name ...string) *Route

func (*Route) Head

func (r *Route) Head(path string, handler Handler, name ...string) *Route

func (*Route) ID added in v1.0.5

func (r *Route) ID() int64

func (*Route) Match

func (r *Route) Match(method string, path []string) (*Route, bool, Variables)

func (*Route) Options

func (r *Route) Options(path string, handler Handler, name ...string) *Route

func (*Route) Patch

func (r *Route) Patch(path string, handler Handler, name ...string) *Route

func (*Route) Post

func (r *Route) Post(path string, handler Handler, name ...string) *Route

func (*Route) Put

func (r *Route) Put(path string, handler Handler, name ...string) *Route

func (*Route) RemoveByPath added in v1.0.4

func (r *Route) RemoveByPath(path string) bool

func (*Route) RemoveChild added in v1.0.4

func (r *Route) RemoveChild(child *Route)

func (*Route) ServeHTTP

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

A function that handles a request.

func (*Route) String

func (r *Route) String() string

String returns a string representation of the route.

func (*Route) Use added in v1.2.0

func (r *Route) Use(middleware ...Middleware) *Route

Use adds middleware to the route.

type Variables

type Variables map[string][]string

func Vars

func Vars(r *http.Request) Variables

func (Variables) Get

func (v Variables) Get(key string) string

func (Variables) GetAll

func (v Variables) GetAll(key string) []string

func (Variables) GetInt

func (v Variables) GetInt(key string) int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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