mirror of
https://github.com/therootcompany/golib.git
synced 2026-01-27 23:18:05 +00:00
105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
// Authored in 2025 by AJ ONeal <aj@therootcompany.com> (https://therootcompany.com)
|
|
//
|
|
// To the extent possible under law, the author(s) have dedicated all copyright
|
|
// and related and neighboring rights to this software to the public domain
|
|
// worldwide. This software is distributed without any warranty.
|
|
//
|
|
// You should have received a copy of the CC0 Public Domain Dedication along with
|
|
// this software. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
|
|
//
|
|
// SPDX-License-Identifier: CC0-1.0
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"slices"
|
|
)
|
|
|
|
// Muxer can mux Handlers and HandleFuncs
|
|
type Muxer interface {
|
|
Handle(path string, handler http.Handler)
|
|
HandleFunc(path string, handle func(w http.ResponseWriter, r *http.Request))
|
|
}
|
|
|
|
// Middleware receives and returns and http.Handler
|
|
type Middleware func(http.Handler) http.Handler
|
|
|
|
// MiddlewareChain enables inline chaining
|
|
type MiddlewareChain struct {
|
|
middlewares []Middleware
|
|
}
|
|
|
|
// Handle composes middleware with the final handler
|
|
func (c MiddlewareChain) Handle(handler http.Handler) http.Handler {
|
|
if handler == nil {
|
|
panic(errors.New("mw.New(...).Use(...).Handle(-->this<--) requires a handler"))
|
|
}
|
|
|
|
middlewares := make([]Middleware, len(c.middlewares))
|
|
copy(middlewares, c.middlewares)
|
|
slices.Reverse(middlewares)
|
|
|
|
// Apply middleware in forward order
|
|
result := handler
|
|
for _, m := range middlewares {
|
|
result = m(result)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// MiddlewareMux enables inline chaining
|
|
type MiddlewareMux struct {
|
|
middlewares []Middleware
|
|
mux Muxer
|
|
}
|
|
|
|
// WithMux wraps a mux such so that Handle and HandleFunc apply the middleware chain
|
|
func WithMux(mux Muxer, middlewares ...Middleware) MiddlewareMux {
|
|
return MiddlewareMux{
|
|
middlewares: middlewares,
|
|
mux: mux,
|
|
}
|
|
}
|
|
|
|
// With creates a new copy of the chain with the specified middleware appended
|
|
func (c MiddlewareMux) With(middlewares ...Middleware) MiddlewareMux {
|
|
newMiddlewares := make([]Middleware, len(c.middlewares), len(c.middlewares)+len(middlewares))
|
|
copy(newMiddlewares, c.middlewares)
|
|
newMiddlewares = append(newMiddlewares, middlewares...)
|
|
|
|
return MiddlewareMux{
|
|
mux: c.mux,
|
|
middlewares: newMiddlewares,
|
|
}
|
|
}
|
|
|
|
func (c MiddlewareMux) Handle(path string, handler http.Handler) {
|
|
c.mux.Handle(path, c.handle(handler))
|
|
}
|
|
|
|
func (c MiddlewareMux) HandleFunc(path string, handler http.HandlerFunc) {
|
|
c.mux.Handle(path, c.handle(handler))
|
|
}
|
|
|
|
// Handle composes middleware with the final handler
|
|
func (c MiddlewareMux) handle(handler http.Handler) http.Handler {
|
|
if handler == nil {
|
|
panic(errors.New("mw.New(...).Use(...).Handle(-->this<--) requires a handler"))
|
|
}
|
|
|
|
middlewares := make([]Middleware, len(c.middlewares))
|
|
copy(middlewares, c.middlewares)
|
|
slices.Reverse(middlewares)
|
|
|
|
// Apply middleware in forward order
|
|
result := handler
|
|
for _, m := range middlewares {
|
|
result = m(result)
|
|
}
|
|
|
|
return result
|
|
}
|