little clean up
This commit is contained in:
40
router.go
40
router.go
@@ -3,27 +3,27 @@ package mux
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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 is a wrapper around the go's standard http.ServeMux.
|
||||
// It's a lean wrapper with methods to make routing easier
|
||||
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.
|
||||
// Use will register middleware(s) with router stack
|
||||
func (r *Router) Use(h ...func(http.Handler) http.Handler) {
|
||||
if r == nil {
|
||||
panic("serve: func Use was called on nil")
|
||||
@@ -31,38 +31,47 @@ func (r *Router) Use(h ...func(http.Handler) http.Handler) {
|
||||
r.middlewares = append(r.middlewares, h...)
|
||||
}
|
||||
|
||||
// Get method route
|
||||
func (r *Router) Get(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodGet, pattern, h)
|
||||
}
|
||||
|
||||
// Head method route
|
||||
func (r *Router) Head(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodHead, pattern, h)
|
||||
}
|
||||
|
||||
// Post method route
|
||||
func (r *Router) Post(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPost, pattern, h)
|
||||
}
|
||||
|
||||
// Put method route
|
||||
func (r *Router) Put(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPut, pattern, h)
|
||||
}
|
||||
|
||||
// Patch method route
|
||||
func (r *Router) Patch(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodPatch, pattern, h)
|
||||
}
|
||||
|
||||
// Delete method route
|
||||
func (r *Router) Delete(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodDelete, pattern, h)
|
||||
}
|
||||
|
||||
// Connect method route
|
||||
func (r *Router) Connect(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodConnect, pattern, h)
|
||||
}
|
||||
|
||||
// Options method route
|
||||
func (r *Router) Options(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodOptions, pattern, h)
|
||||
}
|
||||
|
||||
// Trace method route
|
||||
func (r *Router) Trace(pattern string, h http.HandlerFunc) {
|
||||
r.handlerFunc(http.MethodTrace, pattern, h)
|
||||
}
|
||||
@@ -96,27 +105,38 @@ func (r *Router) With(middleware ...func(http.Handler) http.Handler) *Router {
|
||||
// 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 r == nil {
|
||||
panic("mux: Resource() called on nil")
|
||||
}
|
||||
|
||||
if fn == nil {
|
||||
return
|
||||
panic("mux: Group() requires callback")
|
||||
}
|
||||
|
||||
im := r.With()
|
||||
fn(im)
|
||||
}
|
||||
|
||||
// Route mounts a sub-Router along a `pattern“ string.
|
||||
// Resource resourceful route provides a mapping between HTTP verbs for given the pattern
|
||||
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))
|
||||
panic("mux: Resource() called on nil")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(pattern) == "" {
|
||||
panic("mux: Resource() requires a patter to work")
|
||||
}
|
||||
|
||||
if fn == nil {
|
||||
panic(fmt.Sprintf("chi: attempting to Resource() a nil subrouter on '%s'", pattern))
|
||||
panic("mux: Resource() requires callback")
|
||||
}
|
||||
|
||||
mws := make([]func(http.Handler) http.Handler, len(r.middlewares))
|
||||
copy(mws, r.middlewares)
|
||||
fn(&Resource{
|
||||
mux: r.mux,
|
||||
pattern: pattern,
|
||||
mux: r.mux,
|
||||
pattern: pattern,
|
||||
middlewares: mws,
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user