github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/server/http.go (about)

     1  package server
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gorilla/mux"
     7  )
     8  
     9  type route struct {
    10  	pattern string
    11  	handler http.HandlerFunc
    12  }
    13  
    14  func (ctrl *Controller) addRoutes(router *mux.Router, routes []route,
    15  	middleware ...mux.MiddlewareFunc) {
    16  	for _, r := range routes {
    17  		router.HandleFunc(r.pattern, ctrl.trackMetrics(r.pattern)(chain(r.handler, middleware...)).ServeHTTP)
    18  	}
    19  }
    20  
    21  func chain(f http.Handler, middleware ...mux.MiddlewareFunc) http.Handler {
    22  	if len(middleware) == 0 {
    23  		return f
    24  	}
    25  	return middleware[0](chain(f, middleware[1:cap(middleware)]...))
    26  }