github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/router/chi.go (about)

     1  package router
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/go-chi/chi"
     7  )
     8  
     9  // ChiRouter is an adapter for chi router that implements the Router interface
    10  type ChiRouter struct {
    11  	mux chi.Router
    12  }
    13  
    14  // NewChiRouterWithOptions creates a new instance of ChiRouter
    15  // with the provided options
    16  func NewChiRouterWithOptions(options Options) *ChiRouter {
    17  	router := chi.NewRouter()
    18  	router.NotFound(options.NotFoundHandler)
    19  
    20  	return &ChiRouter{
    21  		mux: router,
    22  	}
    23  }
    24  
    25  // NewChiRouter creates a new instance of ChiRouter
    26  func NewChiRouter() *ChiRouter {
    27  	return NewChiRouterWithOptions(DefaultOptions)
    28  }
    29  
    30  // ServeHTTP server the HTTP requests
    31  func (r *ChiRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    32  	r.mux.ServeHTTP(w, req)
    33  }
    34  
    35  // Any register a path to all HTTP methods
    36  func (r *ChiRouter) Any(path string, handler http.HandlerFunc, handlers ...Constructor) {
    37  	r.with(handlers...).Handle(path, handler)
    38  }
    39  
    40  // Handle registers a path, method and handlers to the router
    41  func (r *ChiRouter) Handle(method string, path string, handler http.HandlerFunc, handlers ...Constructor) {
    42  	switch method {
    43  	case http.MethodGet:
    44  		r.GET(path, handler, handlers...)
    45  	case http.MethodPost:
    46  		r.POST(path, handler, handlers...)
    47  	case http.MethodPut:
    48  		r.PUT(path, handler, handlers...)
    49  	case http.MethodPatch:
    50  		r.PATCH(path, handler, handlers...)
    51  	case http.MethodDelete:
    52  		r.DELETE(path, handler, handlers...)
    53  	case http.MethodHead:
    54  		r.HEAD(path, handler, handlers...)
    55  	case http.MethodOptions:
    56  		r.OPTIONS(path, handler, handlers...)
    57  	}
    58  }
    59  
    60  // GET registers a HTTP GET path
    61  func (r *ChiRouter) GET(path string, handler http.HandlerFunc, handlers ...Constructor) {
    62  	r.with(handlers...).Get(path, handler)
    63  }
    64  
    65  // POST registers a HTTP POST path
    66  func (r *ChiRouter) POST(path string, handler http.HandlerFunc, handlers ...Constructor) {
    67  	r.with(handlers...).Post(path, handler)
    68  }
    69  
    70  // PUT registers a HTTP PUT path
    71  func (r *ChiRouter) PUT(path string, handler http.HandlerFunc, handlers ...Constructor) {
    72  	r.with(handlers...).Put(path, handler)
    73  }
    74  
    75  // DELETE registers a HTTP DELETE path
    76  func (r *ChiRouter) DELETE(path string, handler http.HandlerFunc, handlers ...Constructor) {
    77  	r.with(handlers...).Delete(path, handler)
    78  }
    79  
    80  // PATCH registers a HTTP PATCH path
    81  func (r *ChiRouter) PATCH(path string, handler http.HandlerFunc, handlers ...Constructor) {
    82  	r.with(handlers...).Patch(path, handler)
    83  }
    84  
    85  // HEAD registers a HTTP HEAD path
    86  func (r *ChiRouter) HEAD(path string, handler http.HandlerFunc, handlers ...Constructor) {
    87  	r.with(handlers...).Head(path, handler)
    88  }
    89  
    90  // OPTIONS registers a HTTP OPTIONS path
    91  func (r *ChiRouter) OPTIONS(path string, handler http.HandlerFunc, handlers ...Constructor) {
    92  	r.with(handlers...).Options(path, handler)
    93  }
    94  
    95  // TRACE registers a HTTP TRACE path
    96  func (r *ChiRouter) TRACE(path string, handler http.HandlerFunc, handlers ...Constructor) {
    97  	r.with(handlers...).Trace(path, handler)
    98  }
    99  
   100  // CONNECT registers a HTTP CONNECT path
   101  func (r *ChiRouter) CONNECT(path string, handler http.HandlerFunc, handlers ...Constructor) {
   102  	r.with(handlers...).Connect(path, handler)
   103  }
   104  
   105  // Group creates a child router for a specific path
   106  func (r *ChiRouter) Group(path string) Router {
   107  	return &ChiRouter{r.mux.Route(path, nil)}
   108  }
   109  
   110  // Use attaches a middleware to the router
   111  func (r *ChiRouter) Use(handlers ...Constructor) Router {
   112  	r.mux.Use(r.wrapConstructor(handlers)...)
   113  	return r
   114  }
   115  
   116  // RoutesCount returns number of routes registered
   117  func (r *ChiRouter) RoutesCount() int {
   118  	return r.routesCount(r.mux)
   119  }
   120  
   121  func (r *ChiRouter) routesCount(routes chi.Routes) int {
   122  	count := len(routes.Routes())
   123  	for _, route := range routes.Routes() {
   124  		if nil != route.SubRoutes {
   125  			count += r.routesCount(route.SubRoutes)
   126  		}
   127  	}
   128  	return count
   129  }
   130  
   131  func (r *ChiRouter) with(handlers ...Constructor) chi.Router {
   132  	return r.mux.With(r.wrapConstructor(handlers)...)
   133  }
   134  
   135  func (r *ChiRouter) wrapConstructor(handlers []Constructor) []func(http.Handler) http.Handler {
   136  	var cons = make([]func(http.Handler) http.Handler, 0)
   137  	for _, m := range handlers {
   138  		cons = append(cons, (func(http.Handler) http.Handler)(m))
   139  	}
   140  	return cons
   141  }