github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/router/router.go (about) 1 package router 2 3 import ( 4 "net/http" 5 6 "github.com/go-chi/chi" 7 ) 8 9 // Constructor for a piece of middleware. 10 // Some middleware use this constructor out of the box, 11 // so in most cases you can just pass somepackage.New 12 type Constructor func(http.Handler) http.Handler 13 14 // URLParam returns the url parameter from a http.Request object. 15 func URLParam(r *http.Request, key string) string { 16 return chi.URLParam(r, key) 17 } 18 19 // Router defines the basic methods for a router 20 type Router interface { 21 ServeHTTP(w http.ResponseWriter, req *http.Request) 22 Handle(method string, path string, handler http.HandlerFunc, handlers ...Constructor) 23 Any(path string, handler http.HandlerFunc, handlers ...Constructor) 24 GET(path string, handler http.HandlerFunc, handlers ...Constructor) 25 POST(path string, handler http.HandlerFunc, handlers ...Constructor) 26 PUT(path string, handler http.HandlerFunc, handlers ...Constructor) 27 DELETE(path string, handler http.HandlerFunc, handlers ...Constructor) 28 PATCH(path string, handler http.HandlerFunc, handlers ...Constructor) 29 HEAD(path string, handler http.HandlerFunc, handlers ...Constructor) 30 OPTIONS(path string, handler http.HandlerFunc, handlers ...Constructor) 31 TRACE(path string, handler http.HandlerFunc, handlers ...Constructor) 32 CONNECT(path string, handler http.HandlerFunc, handlers ...Constructor) 33 Group(path string) Router 34 Use(handlers ...Constructor) Router 35 36 RoutesCount() int 37 } 38 39 // Options are the HTTPTreeMuxRouter options 40 type Options struct { 41 NotFoundHandler http.HandlerFunc 42 SafeAddRoutesWhileRunning bool 43 } 44 45 // DefaultOptions are the default router options 46 var DefaultOptions = Options{ 47 NotFoundHandler: http.NotFound, 48 SafeAddRoutesWhileRunning: true, 49 }