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
|
|
|
)
|
|
|
|
|
2024-10-12 15:27:57 +00:00
|
|
|
// Router is a wrapper around the go's standard http.ServeMux.
|
|
|
|
// It's a lean wrapper with methods to make routing easier
|
|
|
|
type Router struct {
|
|
|
|
mux *http.ServeMux
|
|
|
|
middlewares []func(http.Handler) http.Handler
|
|
|
|
}
|
2024-10-12 14:04:17 +00:00
|
|
|
|
|
|
|
func NewRouter() *Router {
|
2024-11-04 05:10:29 +00:00
|
|
|
r := &Router{
|
2024-10-12 14:04:17 +00:00
|
|
|
mux: http.NewServeMux(),
|
|
|
|
}
|
2024-11-04 05:10:29 +00:00
|
|
|
|
|
|
|
// catch all options
|
|
|
|
r.mux.Handle("OPTIONS /", optionsHandler{})
|
|
|
|
return r
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-10-12 15:27:57 +00:00
|
|
|
panic("mux: func Use was called on nil")
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
r.middlewares = append(r.middlewares, h...)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:10:29 +00:00
|
|
|
// GET method route
|
|
|
|
func (r *Router) GET(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
r.handlerFunc(http.MethodGet, pattern, h)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:10:29 +00:00
|
|
|
// HEAD method route
|
|
|
|
func (r *Router) HEAD(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
r.handlerFunc(http.MethodHead, pattern, h)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:10:29 +00:00
|
|
|
// POST method route
|
|
|
|
func (r *Router) POST(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
r.handlerFunc(http.MethodPost, pattern, h)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:10:29 +00:00
|
|
|
// PUT method route
|
|
|
|
func (r *Router) PUT(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
r.handlerFunc(http.MethodPut, pattern, h)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:10:29 +00:00
|
|
|
// PATCH method route
|
|
|
|
func (r *Router) PATCH(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
r.handlerFunc(http.MethodPatch, pattern, h)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:10:29 +00:00
|
|
|
// DELETE method route
|
|
|
|
func (r *Router) DELETE(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
r.handlerFunc(http.MethodDelete, pattern, h)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:10:29 +00:00
|
|
|
// CONNECT method route
|
|
|
|
func (r *Router) CONNECT(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
r.handlerFunc(http.MethodConnect, pattern, h)
|
2024-11-04 05:10:29 +00:00
|
|
|
} // OPTIONS method route
|
|
|
|
func (r *Router) OPTIONS(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
r.handlerFunc(http.MethodOptions, pattern, h)
|
|
|
|
}
|
|
|
|
|
2024-11-04 05:10:29 +00:00
|
|
|
// TRACE method route
|
|
|
|
func (r *Router) TRACE(pattern string, h http.HandlerFunc) {
|
2024-10-12 14:04:17 +00:00
|
|
|
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 {
|
2024-10-12 15:27:57 +00:00
|
|
|
panic("mux: func Handle() was called on nil")
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-10-12 15:27:57 +00:00
|
|
|
panic("mux: method ServeHTTP called on nil")
|
2024-10-12 14:04:17 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 16:54:28 +00:00
|
|
|
r.mux.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
2024-11-03 17:03:56 +00:00
|
|
|
// TODO: proxy for aws lambda
|
2024-10-12 14:04:17 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|