inline middlewares

This commit is contained in:
2025-08-16 14:43:41 +05:30
parent 855b82e9df
commit 216fe93a55
3 changed files with 39 additions and 25 deletions

38
mux.go
View File

@@ -35,37 +35,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 +87,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,13 +101,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 {
mws := make([]func(http.Handler) http.Handler, 0, len(m.middlewares)+len(mw))
copy(mws, m.middlewares)
mws = append(mws, mw...)
} else {
m.mux.Handle(path, stack(m.middlewares, h))
}
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))
mws := make([]func(http.Handler) http.Handler, 0, len(m.middlewares)+len(middleware))
copy(mws, m.middlewares)
mws = append(mws, middleware...)