Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
9d0ab3c0f2 | |||
ec4a0ac231 | |||
5885b42816 | |||
216fe93a55 |
53
mux.go
53
mux.go
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
@@ -35,37 +36,38 @@ func (m *Mux) Use(h ...func(http.Handler) http.Handler) {
|
||||
if m == nil {
|
||||
panic("mux: func Use was called on nil")
|
||||
}
|
||||
|
||||
m.middlewares = append(m.middlewares, h...)
|
||||
}
|
||||
|
||||
// GET method route
|
||||
func (m *Mux) GET(pattern string, h http.HandlerFunc) {
|
||||
m.handle(http.MethodGet, pattern, h)
|
||||
func (m *Mux) GET(pattern string, h http.HandlerFunc, mw ...func(http.Handler) http.Handler) {
|
||||
m.handle(http.MethodGet, pattern, h, mw...)
|
||||
}
|
||||
|
||||
// HEAD method route
|
||||
func (m *Mux) HEAD(pattern string, h http.HandlerFunc) {
|
||||
m.handle(http.MethodHead, pattern, h)
|
||||
func (m *Mux) HEAD(pattern string, h http.HandlerFunc, mw ...func(http.Handler) http.Handler) {
|
||||
m.handle(http.MethodHead, pattern, h, mw...)
|
||||
}
|
||||
|
||||
// POST method route
|
||||
func (m *Mux) POST(pattern string, h http.HandlerFunc) {
|
||||
m.handle(http.MethodPost, pattern, h)
|
||||
func (m *Mux) POST(pattern string, h http.HandlerFunc, mw ...func(http.Handler) http.Handler) {
|
||||
m.handle(http.MethodPost, pattern, h, mw...)
|
||||
}
|
||||
|
||||
// PUT method route
|
||||
func (m *Mux) PUT(pattern string, h http.HandlerFunc) {
|
||||
m.handle(http.MethodPut, pattern, h)
|
||||
func (m *Mux) PUT(pattern string, h http.HandlerFunc, mw ...func(http.Handler) http.Handler) {
|
||||
m.handle(http.MethodPut, pattern, h, mw...)
|
||||
}
|
||||
|
||||
// PATCH method route
|
||||
func (m *Mux) PATCH(pattern string, h http.HandlerFunc) {
|
||||
m.handle(http.MethodPatch, pattern, h)
|
||||
func (m *Mux) PATCH(pattern string, h http.HandlerFunc, mw ...func(http.Handler) http.Handler) {
|
||||
m.handle(http.MethodPatch, pattern, h, mw...)
|
||||
}
|
||||
|
||||
// DELETE method route
|
||||
func (m *Mux) DELETE(pattern string, h http.HandlerFunc) {
|
||||
m.handle(http.MethodDelete, pattern, h)
|
||||
func (m *Mux) DELETE(pattern string, h http.HandlerFunc, mw ...func(http.Handler) http.Handler) {
|
||||
m.handle(http.MethodDelete, pattern, h, mw...)
|
||||
}
|
||||
|
||||
// CONNECT method route
|
||||
@@ -86,7 +88,7 @@ func (m *Mux) TRACE(pattern string, h http.HandlerFunc) {
|
||||
// handle registers the handler for the given pattern.
|
||||
// If the given pattern conflicts, with one that is already registered, HandleFunc
|
||||
// panics.
|
||||
func (m *Mux) handle(method, pattern string, h http.HandlerFunc) {
|
||||
func (m *Mux) handle(method, pattern string, h http.HandlerFunc, mw ...func(http.Handler) http.Handler) {
|
||||
if m == nil {
|
||||
panic("mux: func Handle() was called on nil")
|
||||
}
|
||||
@@ -100,19 +102,20 @@ func (m *Mux) handle(method, pattern string, h http.HandlerFunc) {
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s %s", method, pattern)
|
||||
m.mux.Handle(path, stack(m.middlewares, h))
|
||||
if len(mw) > 0 {
|
||||
m.mux.Handle(path, stack(h, copyMW(m.middlewares, mw)))
|
||||
} else {
|
||||
m.mux.Handle(path, stack(h, m.middlewares))
|
||||
}
|
||||
|
||||
m.routes.Add(path)
|
||||
}
|
||||
|
||||
// With adds inline middlewares for an endpoint handler.
|
||||
func (m *Mux) With(middleware ...func(http.Handler) http.Handler) *Mux {
|
||||
mws := make([]func(http.Handler) http.Handler, len(m.middlewares))
|
||||
copy(mws, m.middlewares)
|
||||
mws = append(mws, middleware...)
|
||||
|
||||
func (m *Mux) With(mw ...func(http.Handler) http.Handler) *Mux {
|
||||
im := &Mux{
|
||||
mux: m.mux,
|
||||
middlewares: mws,
|
||||
middlewares: copyMW(m.middlewares, mw),
|
||||
routes: m.routes,
|
||||
}
|
||||
|
||||
@@ -152,3 +155,13 @@ func (m *Mux) PrintRoutes(w io.Writer) {
|
||||
func (m *Mux) RouteList() []string {
|
||||
return m.routes.All()
|
||||
}
|
||||
|
||||
func copyMW(a []func(http.Handler) http.Handler, b []func(http.Handler) http.Handler) []func(http.Handler) http.Handler {
|
||||
if len(b) > 0 {
|
||||
return slices.Concat(a, b)
|
||||
}
|
||||
|
||||
mws := make([]func(http.Handler) http.Handler, len(a))
|
||||
copy(mws, a)
|
||||
return mws
|
||||
}
|
||||
|
@@ -153,7 +153,8 @@ func TestRouterGroup(t *testing.T) {
|
||||
func TestRouterResource(t *testing.T) {
|
||||
r := New()
|
||||
|
||||
r.Resource("/users", func(res *Resource) {
|
||||
r.Resource("/users",
|
||||
func(res *Resource) {
|
||||
res.Index(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, "All users")
|
||||
})
|
||||
@@ -255,7 +256,7 @@ func TestStack(t *testing.T) {
|
||||
})
|
||||
|
||||
middlewares := []func(http.Handler) http.Handler{middleware1, middleware2}
|
||||
stacked := stack(middlewares, handler)
|
||||
stacked := stack(handler, middlewares)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
|
46
resource.go
46
resource.go
@@ -21,7 +21,7 @@ type Resource struct {
|
||||
// - PUT /pattern/:id update a resource
|
||||
// - PATCH /pattern/:id partial update a resource
|
||||
// - DELETE /resource/:id delete a resource
|
||||
func (m *Mux) Resource(pattern string, fn func(res *Resource)) {
|
||||
func (m *Mux) Resource(pattern string, fn func(res *Resource), mw ...func(http.Handler) http.Handler) {
|
||||
if m == nil {
|
||||
panic("mux: Resource() called on nil")
|
||||
}
|
||||
@@ -34,14 +34,10 @@ func (m *Mux) Resource(pattern string, fn func(res *Resource)) {
|
||||
panic("mux: Resource() requires callback")
|
||||
}
|
||||
|
||||
// Copy root middlewares.
|
||||
mws := make([]func(http.Handler) http.Handler, len(m.middlewares))
|
||||
copy(mws, m.middlewares)
|
||||
|
||||
fn(&Resource{
|
||||
mux: m.mux,
|
||||
pattern: pattern,
|
||||
middlewares: mws,
|
||||
middlewares: copyMW(m.middlewares, mw),
|
||||
routes: m.routes,
|
||||
})
|
||||
}
|
||||
@@ -106,10 +102,38 @@ func (res *Resource) Delete(h http.HandlerFunc) {
|
||||
res.handlerFunc(http.MethodDelete, p, h)
|
||||
}
|
||||
|
||||
func (res *Resource) Handle(pattern string, h http.HandlerFunc) {
|
||||
p := suffixIt(res.pattern, "{id}")
|
||||
res.routes.Add(http.MethodDelete + " " + p)
|
||||
res.handlerFunc(http.MethodDelete, p, h)
|
||||
// HandleGET on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandleGET(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodGet, pattern, h)
|
||||
}
|
||||
|
||||
// HandlePOST on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandlePOST(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodPost, pattern, h)
|
||||
}
|
||||
|
||||
// HandlePUT on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandlePUT(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodPut, pattern, h)
|
||||
}
|
||||
|
||||
// HandlePATCH on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandlePATCH(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodPatch, pattern, h)
|
||||
}
|
||||
|
||||
// HandleDELETE on /group-pattern/:id/pattern
|
||||
func (res *Resource) HandleDELETE(pattern string, h http.HandlerFunc) {
|
||||
res.handle(http.MethodDelete, pattern, h)
|
||||
}
|
||||
|
||||
func (res *Resource) handle(method string, pattern string, h http.HandlerFunc) {
|
||||
if !strings.HasPrefix(pattern, "/") {
|
||||
pattern = "/" + pattern
|
||||
}
|
||||
p := suffixIt(res.pattern, "{id}"+pattern)
|
||||
res.routes.Add(method + " " + p)
|
||||
res.handlerFunc(method, p, h)
|
||||
}
|
||||
|
||||
// handlerFunc registers the handler function for the given pattern.
|
||||
@@ -125,7 +149,7 @@ func (res *Resource) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s %s", method, pattern)
|
||||
res.mux.Handle(path, stack(res.middlewares, h))
|
||||
res.mux.Handle(path, stack(h, res.middlewares))
|
||||
}
|
||||
|
||||
// Use will register middleware(s) on Router stack.
|
||||
|
2
stack.go
2
stack.go
@@ -3,7 +3,7 @@ package mux
|
||||
import "net/http"
|
||||
|
||||
// stack middlewares(http handler) in order they are passed (FIFO)
|
||||
func stack(middlewares []func(http.Handler) http.Handler, endpoint http.Handler) http.Handler {
|
||||
func stack(endpoint http.Handler, middlewares []func(http.Handler) http.Handler) http.Handler {
|
||||
// Return ahead of time if there aren't any middlewares for the chain
|
||||
if len(middlewares) == 0 {
|
||||
return endpoint
|
||||
|
Reference in New Issue
Block a user