alien
v0.1.0
Documentation
pkg.go.dev ↗Package alien is a lightweight http router from outer space.
Functions
ParseParams
func ParseParams(matched, pattern string) (result string, err error)
ParseParams parses params found in mateched from pattern. There are two kinds of params, one to capture a segment which starts with : and a nother to capture everything( a.k.a catch all) whis starts with *.
For instance
pattern:="/hello/:name" matched:="/hello/world"
Will result into name:world. this function captures the named params and theri coreesponding values, returning them in a comma separated string of a key:value nature. please see the tests for more details.
Types
Mux
type Mux struct {
// contains filtered or unexported fields
}
Mux is a http multiplexer that allows matching of http requests to the registered http handlers.
Mux supports named parameters in urls like
/hello/:name
will match
/hello/world
where by inside the request passed to the handler, the param with key name and value world will be passed.
Mux supports catch all parameters too
/hello/*whatever
will match
/hello/world /hello/world/tanzania /hello/world/afica/tanzania.png
where by inside the request passed to the handler, the param with key whatever will be set and value will be
world world/tanzania world/afica/tanzania.png
If you dont specify a name in a catch all route, then the default name "catch" will be ussed.
New
func New() *Mux
New returns a new *Mux instance with default handler for mismatched routes.
Mux.AddRoute
func (m *Mux) AddRoute(method, pattern string, h func(http.ResponseWriter, *http.Request)) error
AddRoute registers h with pattern and method. If there is a path prefix created via the Group method) it will be set.
Mux.Connect
func (m *Mux) Connect(path string, h func(http.ResponseWriter, *http.Request)) error
Connect registers h with pattern and method CONNECT.
Mux.Delete
func (m *Mux) Delete(path string, h func(http.ResponseWriter, *http.Request)) error
Delete registers h with pattern and method DELETE.
Mux.Get
func (m *Mux) Get(pattern string, h func(http.ResponseWriter, *http.Request)) error
Get registers h with pattern and method GET.
Mux.Group
func (m *Mux) Group(pattern string) *Mux
Group creates a path prefix group for pattern, all routes registered using the returned Mux will only match if the request path starts with pattern. For instance .
m:=New()
home:=m.Group("/home")
home.Get("/alone",myHandler)
will match
/home/alone
Mux.Head
func (m *Mux) Head(path string, h func(http.ResponseWriter, *http.Request)) error
Head registers h with pattern and method HEAD.
Mux.NotFoundHandler
func (m *Mux) NotFoundHandler(h http.Handler)
NotFoundHandler is executed when the request route is not found.
Mux.Options
func (m *Mux) Options(path string, h func(http.ResponseWriter, *http.Request)) error
Options registers h with pattern and method OPTIONS.
Mux.Patch
func (m *Mux) Patch(path string, h func(http.ResponseWriter, *http.Request)) error
Patch registers h with pattern and method PATCH.
Mux.Post
func (m *Mux) Post(path string, h func(http.ResponseWriter, *http.Request)) error
Post registers h with pattern and method POST.
Mux.Put
func (m *Mux) Put(path string, h func(http.ResponseWriter, *http.Request)) error
Put registers h with pattern and method PUT.
Mux.ServeHTTP
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler interface. It muliplexes http requests against registered handlers.
Mux.Trace
func (m *Mux) Trace(path string, h func(http.ResponseWriter, *http.Request)) error
Trace registers h with pattern and method TRACE.
Mux.Use
func (m *Mux) Use(middleware ...func(http.Handler) http.Handler)
Use assigns midlewares to the current *Mux. All routes registered by the *Mux after this call will have the middlewares assigned to them.
Params
type Params map[string]string
Params stores route params.
GetParams
func GetParams(r *http.Request) Params
GetParams returrns route params stored in r.
Params.Get
func (p Params) Get(key string) string
Get returns value associated with key.
Params.Load
func (p Params) Load(src string)
Load loads params found in src into p.