router with helper methods
This commit is contained in:
145
router.go
Normal file
145
router.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package mux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const RouteCtxKey = "ServeCTX"
|
||||
|
||||
type (
|
||||
// Router is a wrapper arround the go's standard http.ServeMux.
|
||||
// Its a lean wrapper with feature that are there to make easy use of it.
|
||||
Router struct {
|
||||
mux *http.ServeMux
|
||||
middlewares []func(http.Handler) http.Handler
|
||||
}
|
||||
)
|
||||
|
||||
// New instance of Mux
|
||||
func NewRouter() *Router {
|
||||
return &Router{
|
||||
mux: http.NewServeMux(),
|
||||
}
|
||||
}
|
||||
|
||||
// Use appends one or more middlewares onto the Router stack.
|
||||
func (r *Router) Use(h ...func(http.Handler) http.Handler) {
|
||||
if r == nil {
|
||||
panic("serve: func Use was called on nil")
|
||||
}
|
||||
r.middlewares = append(r.middlewares, h...)
|
||||
}
|
||||
|
||||
func (r *Router) Get(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, pattern, h)
|
||||
}
|
||||
|
||||
func (r *Router) Head(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodHead, pattern, h)
|
||||
}
|
||||
|
||||
func (r *Router) Post(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPost, pattern, h)
|
||||
}
|
||||
|
||||
func (r *Router) Put(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPut, pattern, h)
|
||||
}
|
||||
|
||||
func (r *Router) Patch(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPatch, pattern, h)
|
||||
}
|
||||
|
||||
func (r *Router) Delete(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodDelete, pattern, h)
|
||||
}
|
||||
|
||||
func (r *Router) Connect(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodConnect, pattern, h)
|
||||
}
|
||||
|
||||
func (r *Router) Options(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodOptions, pattern, h)
|
||||
}
|
||||
|
||||
func (r *Router) Trace(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodTrace, pattern, h)
|
||||
}
|
||||
|
||||
// HandleFunc registers the handler function for the given pattern.
|
||||
// If the given pattern conflicts, with one that is already registered, HandleFunc
|
||||
// panics.
|
||||
func (r *Router) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
||||
if r == nil {
|
||||
panic("serve: func Handle() was called on nil")
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s %s", method, pattern)
|
||||
r.mux.Handle(path, stack(r.middlewares, h))
|
||||
}
|
||||
|
||||
// With adds inline middlewares for an endpoint handler.
|
||||
func (r *Router) With(middleware ...func(http.Handler) http.Handler) *Router {
|
||||
mws := make([]func(http.Handler) http.Handler, len(r.middlewares))
|
||||
copy(mws, r.middlewares)
|
||||
mws = append(mws, middleware...)
|
||||
|
||||
im := &Router{
|
||||
mux: r.mux,
|
||||
middlewares: mws,
|
||||
}
|
||||
|
||||
return im
|
||||
}
|
||||
|
||||
// Group adds a new inline-Router along the current routing
|
||||
// path, with a fresh middleware stack for the inline-Router.
|
||||
func (r *Router) Group(fn func(grp *Router)) {
|
||||
if fn == nil {
|
||||
return
|
||||
}
|
||||
|
||||
im := r.With()
|
||||
fn(im)
|
||||
}
|
||||
|
||||
// Route mounts a sub-Router along a `pattern“ string.
|
||||
func (r *Router) Resource(pattern string, fn func(resource *Resource)) {
|
||||
if r == nil {
|
||||
panic(fmt.Sprintf("chi: attempting to Route() a nil subrouter on '%s'", pattern))
|
||||
}
|
||||
|
||||
if fn == nil {
|
||||
panic(fmt.Sprintf("chi: attempting to Resource() a nil subrouter on '%s'", pattern))
|
||||
}
|
||||
|
||||
fn(&Resource{
|
||||
mux: r.mux,
|
||||
pattern: pattern,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if r == nil {
|
||||
panic("method ServeHTTP called on nil")
|
||||
}
|
||||
|
||||
r.mux.ServeHTTP(w, req)
|
||||
}
|
||||
|
||||
// stack middlewares(http handler) in order they are passed (FIFO)
|
||||
func stack(middlewares []func(http.Handler) http.Handler, endpoint http.Handler) http.Handler {
|
||||
// Return ahead of time if there aren't any middlewares for the chain
|
||||
if len(middlewares) == 0 {
|
||||
return endpoint
|
||||
}
|
||||
|
||||
// wrap the end handler with the middleware chain
|
||||
h := middlewares[len(middlewares)-1](endpoint)
|
||||
for i := len(middlewares) - 2; i >= 0; i-- {
|
||||
h = middlewares[i](h)
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
Reference in New Issue
Block a user