extract middlewares to lightmux-contrib

Removes pkg/middleware. The Logger, Recoverer, and RealIP middlewares
now live in the sibling lightmux-contrib module as the realip,
requestlog, and recoverer packages, each exposing a single New(...)
constructor.

The Middleware type alias moves to pkg/router. The splinter dependency
is dropped from go.mod; only errx remains.

BREAKING CHANGE: consumers must replace pkg/middleware imports with
the corresponding lightmux-contrib sub-packages. See README for the
new usage.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
juancwu 2026-04-26 14:02:54 +00:00
commit 22277186ae
15 changed files with 44 additions and 612 deletions

View file

@ -4,16 +4,12 @@
// underlying mux while carrying their own prefix and middleware stack.
package router
import (
"net/http"
"git.juancwu.dev/juancwu/lightmux/pkg/middleware"
)
import "net/http"
type Mux struct {
root *http.ServeMux
prefix string
middlewares []middleware.Middleware
middlewares []Middleware
}
func New() *Mux {
@ -21,16 +17,16 @@ func New() *Mux {
return &Mux{root: sm}
}
func (m *Mux) Use(mws ...middleware.Middleware) {
func (m *Mux) Use(mws ...Middleware) {
m.middlewares = append(m.middlewares, mws...)
}
// Group returns a child Mux that registers on the same underlying ServeMux but
// with its prefix appended and the parent's current middlewares snapshotted.
// Use() calls made on the parent after Group() do not propagate to the child.
func (m *Mux) Group(prefix string, mws ...middleware.Middleware) *Mux {
func (m *Mux) Group(prefix string, mws ...Middleware) *Mux {
validateGroupPrefix(prefix)
mwsCopy := make([]middleware.Middleware, 0, len(m.middlewares)+len(mws))
mwsCopy := make([]Middleware, 0, len(m.middlewares)+len(mws))
mwsCopy = append(mwsCopy, m.middlewares...)
mwsCopy = append(mwsCopy, mws...)
return &Mux{
@ -40,45 +36,45 @@ func (m *Mux) Group(prefix string, mws ...middleware.Middleware) *Mux {
}
}
func (m *Mux) Handle(pattern string, h http.Handler, mws ...middleware.Middleware) {
func (m *Mux) Handle(pattern string, h http.Handler, mws ...Middleware) {
full := buildPattern("", m.prefix, pattern)
m.root.Handle(full, chain(h, m.middlewares, mws))
}
func (m *Mux) HandleFunc(pattern string, fn http.HandlerFunc, mws ...middleware.Middleware) {
func (m *Mux) HandleFunc(pattern string, fn http.HandlerFunc, mws ...Middleware) {
m.Handle(pattern, fn, mws...)
}
func (m *Mux) method(method, path string, fn http.HandlerFunc, mws []middleware.Middleware) {
func (m *Mux) method(method, path string, fn http.HandlerFunc, mws []Middleware) {
full := buildPattern(method, m.prefix, path)
m.root.Handle(full, chain(fn, m.middlewares, mws))
}
func (m *Mux) Get(path string, fn http.HandlerFunc, mws ...middleware.Middleware) {
func (m *Mux) Get(path string, fn http.HandlerFunc, mws ...Middleware) {
m.method(http.MethodGet, path, fn, mws)
}
func (m *Mux) Post(path string, fn http.HandlerFunc, mws ...middleware.Middleware) {
func (m *Mux) Post(path string, fn http.HandlerFunc, mws ...Middleware) {
m.method(http.MethodPost, path, fn, mws)
}
func (m *Mux) Put(path string, fn http.HandlerFunc, mws ...middleware.Middleware) {
func (m *Mux) Put(path string, fn http.HandlerFunc, mws ...Middleware) {
m.method(http.MethodPut, path, fn, mws)
}
func (m *Mux) Patch(path string, fn http.HandlerFunc, mws ...middleware.Middleware) {
func (m *Mux) Patch(path string, fn http.HandlerFunc, mws ...Middleware) {
m.method(http.MethodPatch, path, fn, mws)
}
func (m *Mux) Delete(path string, fn http.HandlerFunc, mws ...middleware.Middleware) {
func (m *Mux) Delete(path string, fn http.HandlerFunc, mws ...Middleware) {
m.method(http.MethodDelete, path, fn, mws)
}
func (m *Mux) Options(path string, fn http.HandlerFunc, mws ...middleware.Middleware) {
func (m *Mux) Options(path string, fn http.HandlerFunc, mws ...Middleware) {
m.method(http.MethodOptions, path, fn, mws)
}
func (m *Mux) Head(path string, fn http.HandlerFunc, mws ...middleware.Middleware) {
func (m *Mux) Head(path string, fn http.HandlerFunc, mws ...Middleware) {
m.method(http.MethodHead, path, fn, mws)
}