package mux import ( "fmt" "net/http" "strings" ) // 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 } func NewRouter() *Router { r := &Router{ mux: http.NewServeMux(), } // catch all options r.mux.Handle("OPTIONS /", optionsHandler{}) return r } // Use will register middleware(s) with router stack func (r *Router) Use(h ...func(http.Handler) http.Handler) { if r == nil { panic("mux: func Use was called on nil") } r.middlewares = append(r.middlewares, h...) } // GET method route func (r *Router) GET(pattern string, h http.HandlerFunc) { r.handlerFunc(http.MethodGet, pattern, h) } // HEAD method route func (r *Router) HEAD(pattern string, h http.HandlerFunc) { r.handlerFunc(http.MethodHead, pattern, h) } // POST method route func (r *Router) POST(pattern string, h http.HandlerFunc) { r.handlerFunc(http.MethodPost, pattern, h) } // PUT method route func (r *Router) PUT(pattern string, h http.HandlerFunc) { r.handlerFunc(http.MethodPut, pattern, h) } // PATCH method route func (r *Router) PATCH(pattern string, h http.HandlerFunc) { r.handlerFunc(http.MethodPatch, pattern, h) } // DELETE method route func (r *Router) DELETE(pattern string, h http.HandlerFunc) { r.handlerFunc(http.MethodDelete, pattern, h) } // CONNECT method route func (r *Router) CONNECT(pattern string, h http.HandlerFunc) { r.handlerFunc(http.MethodConnect, pattern, h) } // OPTIONS method route func (r *Router) OPTIONS(pattern string, h http.HandlerFunc) { r.handlerFunc(http.MethodOptions, pattern, h) } // TRACE method route 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("mux: 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)) { if r == nil { panic("mux: Resource() called on nil") } if fn == nil { panic("mux: Group() requires callback") } im := r.With() fn(im) } // Resource resourceful route provides a mapping between HTTP verbs for given the pattern func (r *Router) Resource(pattern string, fn func(resource *Resource)) { if r == nil { panic("mux: Resource() called on nil") } if strings.TrimSpace(pattern) == "" { panic("mux: Resource() requires a patter to work") } if fn == nil { panic("mux: Resource() requires callback") } mws := make([]func(http.Handler) http.Handler, len(r.middlewares)) copy(mws, r.middlewares) fn(&Resource{ mux: r.mux, pattern: pattern, middlewares: mws, }) } func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { if r == nil { panic("mux: method ServeHTTP called on nil") } r.mux.ServeHTTP(w, req) } // TODO: proxy for aws lambda // 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 }