mux/router.go

166 lines
4.0 KiB
Go
Raw Normal View History

2024-10-12 14:04:17 +00:00
package mux
import (
"fmt"
"net/http"
2024-10-12 14:43:33 +00:00
"strings"
2024-10-12 14:04:17 +00:00
)
const RouteCtxKey = "ServeCTX"
type (
2024-10-12 14:43:33 +00:00
// Router is a wrapper around the go's standard http.ServeMux.
// It's a lean wrapper with methods to make routing easier
2024-10-12 14:04:17 +00:00
Router struct {
mux *http.ServeMux
middlewares []func(http.Handler) http.Handler
}
)
func NewRouter() *Router {
return &Router{
mux: http.NewServeMux(),
}
}
2024-10-12 14:43:33 +00:00
// Use will register middleware(s) with router stack
2024-10-12 14:04:17 +00:00
func (r *Router) Use(h ...func(http.Handler) http.Handler) {
if r == nil {
panic("serve: func Use was called on nil")
}
r.middlewares = append(r.middlewares, h...)
}
2024-10-12 14:43:33 +00:00
// Get method route
2024-10-12 14:04:17 +00:00
func (r *Router) Get(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodGet, pattern, h)
}
2024-10-12 14:43:33 +00:00
// Head method route
2024-10-12 14:04:17 +00:00
func (r *Router) Head(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodHead, pattern, h)
}
2024-10-12 14:43:33 +00:00
// Post method route
2024-10-12 14:04:17 +00:00
func (r *Router) Post(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPost, pattern, h)
}
2024-10-12 14:43:33 +00:00
// Put method route
2024-10-12 14:04:17 +00:00
func (r *Router) Put(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPut, pattern, h)
}
2024-10-12 14:43:33 +00:00
// Patch method route
2024-10-12 14:04:17 +00:00
func (r *Router) Patch(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodPatch, pattern, h)
}
2024-10-12 14:43:33 +00:00
// Delete method route
2024-10-12 14:04:17 +00:00
func (r *Router) Delete(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodDelete, pattern, h)
}
2024-10-12 14:43:33 +00:00
// Connect method route
2024-10-12 14:04:17 +00:00
func (r *Router) Connect(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodConnect, pattern, h)
}
2024-10-12 14:43:33 +00:00
// Options method route
2024-10-12 14:04:17 +00:00
func (r *Router) Options(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodOptions, pattern, h)
}
2024-10-12 14:43:33 +00:00
// Trace method route
2024-10-12 14:04:17 +00:00
func (r *Router) Trace(pattern string, h http.HandlerFunc) {
r.handlerFunc(http.MethodTrace, pattern, h)
}
// HandleFunc registers the handler function for the given pattern.
// If the given pattern conflicts, with one that is already registered, HandleFunc
// panics.
func (r *Router) handlerFunc(method, pattern string, h http.HandlerFunc) {
if r == nil {
panic("serve: func Handle() was called on nil")
}
path := fmt.Sprintf("%s %s", method, pattern)
r.mux.Handle(path, stack(r.middlewares, h))
}
// With adds inline middlewares for an endpoint handler.
func (r *Router) With(middleware ...func(http.Handler) http.Handler) *Router {
mws := make([]func(http.Handler) http.Handler, len(r.middlewares))
copy(mws, r.middlewares)
mws = append(mws, middleware...)
im := &Router{
mux: r.mux,
middlewares: mws,
}
return im
}
// Group adds a new inline-Router along the current routing
// path, with a fresh middleware stack for the inline-Router.
func (r *Router) Group(fn func(grp *Router)) {
2024-10-12 14:43:33 +00:00
if r == nil {
panic("mux: Resource() called on nil")
}
2024-10-12 14:04:17 +00:00
if fn == nil {
2024-10-12 14:43:33 +00:00
panic("mux: Group() requires callback")
2024-10-12 14:04:17 +00:00
}
im := r.With()
fn(im)
}
2024-10-12 14:43:33 +00:00
// Resource resourceful route provides a mapping between HTTP verbs for given the pattern
2024-10-12 14:04:17 +00:00
func (r *Router) Resource(pattern string, fn func(resource *Resource)) {
if r == nil {
2024-10-12 14:43:33 +00:00
panic("mux: Resource() called on nil")
}
if strings.TrimSpace(pattern) == "" {
panic("mux: Resource() requires a patter to work")
2024-10-12 14:04:17 +00:00
}
if fn == nil {
2024-10-12 14:43:33 +00:00
panic("mux: Resource() requires callback")
2024-10-12 14:04:17 +00:00
}
2024-10-12 14:43:33 +00:00
mws := make([]func(http.Handler) http.Handler, len(r.middlewares))
copy(mws, r.middlewares)
2024-10-12 14:04:17 +00:00
fn(&Resource{
2024-10-12 14:43:33 +00:00
mux: r.mux,
pattern: pattern,
middlewares: mws,
2024-10-12 14:04:17 +00:00
})
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if r == nil {
panic("method ServeHTTP called on nil")
}
r.mux.ServeHTTP(w, req)
}
// stack middlewares(http handler) in order they are passed (FIFO)
func stack(middlewares []func(http.Handler) http.Handler, endpoint http.Handler) http.Handler {
// Return ahead of time if there aren't any middlewares for the chain
if len(middlewares) == 0 {
return endpoint
}
// wrap the end handler with the middleware chain
h := middlewares[len(middlewares)-1](endpoint)
for i := len(middlewares) - 2; i >= 0; i-- {
h = middlewares[i](h)
}
return h
}