lightmux/pkg/router/mux.go
juancwu 22277186ae 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>
2026-04-26 14:02:54 +00:00

83 lines
2.5 KiB
Go

// Package router provides a thin, idiomatic wrapper around the Go 1.22+
// net/http ServeMux. It adds method-named convenience methods (Get, Post,
// ...), per-route and group middleware, and Group sub-routers that share the
// underlying mux while carrying their own prefix and middleware stack.
package router
import "net/http"
type Mux struct {
root *http.ServeMux
prefix string
middlewares []Middleware
}
func New() *Mux {
sm := http.NewServeMux()
return &Mux{root: sm}
}
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) *Mux {
validateGroupPrefix(prefix)
mwsCopy := make([]Middleware, 0, len(m.middlewares)+len(mws))
mwsCopy = append(mwsCopy, m.middlewares...)
mwsCopy = append(mwsCopy, mws...)
return &Mux{
root: m.root,
prefix: m.prefix + normalizeGroupPrefix(prefix),
middlewares: mwsCopy,
}
}
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) {
m.Handle(pattern, fn, mws...)
}
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) {
m.method(http.MethodGet, path, fn, mws)
}
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) {
m.method(http.MethodPut, path, fn, mws)
}
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) {
m.method(http.MethodDelete, path, fn, mws)
}
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) {
m.method(http.MethodHead, path, fn, mws)
}
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.root.ServeHTTP(w, r)
}