Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
9d0ab3c0f2 | |||
ec4a0ac231 | |||
5885b42816 |
25
mux.go
25
mux.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
@@ -102,25 +103,19 @@ func (m *Mux) handle(method, pattern string, h http.HandlerFunc, mw ...func(http
|
|||||||
|
|
||||||
path := fmt.Sprintf("%s %s", method, pattern)
|
path := fmt.Sprintf("%s %s", method, pattern)
|
||||||
if len(mw) > 0 {
|
if len(mw) > 0 {
|
||||||
mws := make([]func(http.Handler) http.Handler, 0, len(m.middlewares)+len(mw))
|
m.mux.Handle(path, stack(h, copyMW(m.middlewares, mw)))
|
||||||
copy(mws, m.middlewares)
|
|
||||||
mws = append(mws, mw...)
|
|
||||||
} else {
|
} else {
|
||||||
m.mux.Handle(path, stack(m.middlewares, h))
|
m.mux.Handle(path, stack(h, m.middlewares))
|
||||||
}
|
}
|
||||||
|
|
||||||
m.routes.Add(path)
|
m.routes.Add(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// With adds inline middlewares for an endpoint handler.
|
// With adds inline middlewares for an endpoint handler.
|
||||||
func (m *Mux) With(middleware ...func(http.Handler) http.Handler) *Mux {
|
func (m *Mux) With(mw ...func(http.Handler) http.Handler) *Mux {
|
||||||
mws := make([]func(http.Handler) http.Handler, 0, len(m.middlewares)+len(middleware))
|
|
||||||
copy(mws, m.middlewares)
|
|
||||||
mws = append(mws, middleware...)
|
|
||||||
|
|
||||||
im := &Mux{
|
im := &Mux{
|
||||||
mux: m.mux,
|
mux: m.mux,
|
||||||
middlewares: mws,
|
middlewares: copyMW(m.middlewares, mw),
|
||||||
routes: m.routes,
|
routes: m.routes,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,3 +155,13 @@ func (m *Mux) PrintRoutes(w io.Writer) {
|
|||||||
func (m *Mux) RouteList() []string {
|
func (m *Mux) RouteList() []string {
|
||||||
return m.routes.All()
|
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
|
||||||
|
}
|
||||||
|
@@ -256,7 +256,7 @@ func TestStack(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
middlewares := []func(http.Handler) http.Handler{middleware1, middleware2}
|
middlewares := []func(http.Handler) http.Handler{middleware1, middleware2}
|
||||||
stacked := stack(middlewares, handler)
|
stacked := stack(handler, middlewares)
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
49
resource.go
49
resource.go
@@ -34,19 +34,10 @@ func (m *Mux) Resource(pattern string, fn func(res *Resource), mw ...func(http.H
|
|||||||
panic("mux: Resource() requires callback")
|
panic("mux: Resource() requires callback")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy root middlewares.
|
|
||||||
mws := make([]func(http.Handler) http.Handler, 0, len(m.middlewares)+len(mw))
|
|
||||||
copy(mws, m.middlewares)
|
|
||||||
|
|
||||||
// Append inline middlewares.
|
|
||||||
if len(mw) > 0 {
|
|
||||||
mws = append(mws, mw...)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn(&Resource{
|
fn(&Resource{
|
||||||
mux: m.mux,
|
mux: m.mux,
|
||||||
pattern: pattern,
|
pattern: pattern,
|
||||||
middlewares: mws,
|
middlewares: copyMW(m.middlewares, mw),
|
||||||
routes: m.routes,
|
routes: m.routes,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -111,10 +102,38 @@ func (res *Resource) Delete(h http.HandlerFunc) {
|
|||||||
res.handlerFunc(http.MethodDelete, p, h)
|
res.handlerFunc(http.MethodDelete, p, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (res *Resource) Handle(pattern string, h http.HandlerFunc) {
|
// HandleGET on /group-pattern/:id/pattern
|
||||||
p := suffixIt(res.pattern, "{id}")
|
func (res *Resource) HandleGET(pattern string, h http.HandlerFunc) {
|
||||||
res.routes.Add(http.MethodDelete + " " + p)
|
res.handle(http.MethodGet, pattern, h)
|
||||||
res.handlerFunc(http.MethodDelete, p, 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.
|
// handlerFunc registers the handler function for the given pattern.
|
||||||
@@ -130,7 +149,7 @@ func (res *Resource) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
path := fmt.Sprintf("%s %s", method, pattern)
|
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.
|
// Use will register middleware(s) on Router stack.
|
||||||
|
2
stack.go
2
stack.go
@@ -3,7 +3,7 @@ package mux
|
|||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
// stack middlewares(http handler) in order they are passed (FIFO)
|
// 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
|
// Return ahead of time if there aren't any middlewares for the chain
|
||||||
if len(middlewares) == 0 {
|
if len(middlewares) == 0 {
|
||||||
return endpoint
|
return endpoint
|
||||||
|
Reference in New Issue
Block a user