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

     1  package router
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gorilla/mux"
     7  	"github.com/sirupsen/logrus"
     8  
     9  	"github.com/pyroscope-io/pyroscope/pkg/api"
    10  	"github.com/pyroscope-io/pyroscope/pkg/api/authz"
    11  	"github.com/pyroscope-io/pyroscope/pkg/server/httputils"
    12  )
    13  
    14  type Router struct {
    15  	*mux.Router
    16  	Services
    17  }
    18  
    19  type Services struct {
    20  	Logger logrus.FieldLogger
    21  
    22  	api.AuthService
    23  	api.UserService
    24  	api.APIKeyService
    25  	api.AnnotationsService
    26  	api.AdhocService
    27  	api.ApplicationListerAndDeleter
    28  }
    29  
    30  func New(m *mux.Router, s Services) *Router {
    31  	return &Router{
    32  		Router:   m,
    33  		Services: s,
    34  	}
    35  }
    36  
    37  func (r *Router) RegisterHandlers() {
    38  	r.RegisterUserHandlers()
    39  	r.RegisterAPIKeyHandlers()
    40  	r.RegisterAnnotationsHandlers()
    41  }
    42  
    43  func (r *Router) RegisterUserHandlers() {
    44  	h := api.NewUserHandler(r.UserService, httputils.NewDefaultHelper(r.Logger))
    45  	authorizer := authz.NewAuthorizer(r.Services.Logger, httputils.NewDefaultHelper(r.Logger))
    46  
    47  	x := r.PathPrefix("/users").Subrouter()
    48  	x.Use(authorizer.RequireAdminRole)
    49  	x.Methods(http.MethodPost).HandlerFunc(h.CreateUser)
    50  	x.Methods(http.MethodGet).HandlerFunc(h.ListUsers)
    51  
    52  	x = x.PathPrefix("/{id:[0-9]+}").Subrouter()
    53  	x.Methods(http.MethodGet).HandlerFunc(h.GetUser)
    54  	x.Methods(http.MethodPatch).HandlerFunc(h.UpdateUser)
    55  	x.Methods(http.MethodDelete).HandlerFunc(h.DeleteUser)
    56  	x.Path("/password").Methods(http.MethodPut).HandlerFunc(h.ChangeUserPassword)
    57  	x.Path("/disable").Methods(http.MethodPut).HandlerFunc(h.DisableUser)
    58  	x.Path("/enable").Methods(http.MethodPut).HandlerFunc(h.EnableUser)
    59  	x.Path("/role").Methods(http.MethodPut).HandlerFunc(h.ChangeUserRole)
    60  
    61  	// Endpoints available to all authenticated users.
    62  	x = r.PathPrefix("/user").Subrouter()
    63  	x.Use(authorizer.RequireAuthenticatedUser)
    64  	x.Methods(http.MethodGet).HandlerFunc(h.GetAuthenticatedUser)
    65  	x.Methods(http.MethodPatch).HandlerFunc(h.UpdateAuthenticatedUser)
    66  	x.Path("/password").Methods(http.MethodPut).HandlerFunc(h.ChangeAuthenticatedUserPassword)
    67  }
    68  
    69  func (r *Router) RegisterAPIKeyHandlers() {
    70  	h := api.NewAPIKeyHandler(r.Logger, r.APIKeyService, httputils.NewDefaultHelper(r.Logger))
    71  	authorizer := authz.NewAuthorizer(r.Services.Logger, httputils.NewDefaultHelper(r.Logger))
    72  
    73  	x := r.PathPrefix("/keys").Subrouter()
    74  	x.Use(authorizer.RequireAdminRole)
    75  	x.Methods(http.MethodPost).HandlerFunc(h.CreateAPIKey)
    76  	x.Methods(http.MethodGet).HandlerFunc(h.ListAPIKeys)
    77  
    78  	x = x.PathPrefix("/{id:[0-9]+}").Subrouter()
    79  	x.Methods(http.MethodDelete).HandlerFunc(h.DeleteAPIKey)
    80  }
    81  
    82  func (r *Router) RegisterAnnotationsHandlers() {
    83  	h := api.NewAnnotationsHandler(r.AnnotationsService, httputils.NewDefaultHelper(r.Logger))
    84  
    85  	x := r.PathPrefix("/annotations").Subrouter()
    86  	x.Methods(http.MethodPost).HandlerFunc(h.CreateAnnotation)
    87  }
    88  
    89  func (r *Router) RegisterAdhocHandlers(maxFileSize int64) {
    90  	h := api.NewAdhocHandler(r.AdhocService, httputils.NewDefaultHelper(r.Logger), maxFileSize)
    91  
    92  	x := r.PathPrefix("/adhoc/v1").Subrouter()
    93  	x.Methods(http.MethodGet).PathPrefix("/profiles").HandlerFunc(h.GetProfiles)
    94  	x.Methods(http.MethodGet).PathPrefix("/profile/{id:[0-9a-f]+}").HandlerFunc(h.GetProfile)
    95  	x.Methods(http.MethodGet).PathPrefix("/diff/{left:[0-9a-f]+}/{right:[0-9a-f]+}").HandlerFunc(h.GetProfileDiff)
    96  	x.Methods(http.MethodPost).PathPrefix("/upload").HandlerFunc(h.Upload)
    97  }
    98  
    99  func (r *Router) RegisterApplicationHandlers() {
   100  	h := api.NewApplicationsHandler(r.ApplicationListerAndDeleter, httputils.NewDefaultHelper(r.Logger))
   101  	authorizer := authz.NewAuthorizer(r.Services.Logger, httputils.NewDefaultHelper(r.Logger))
   102  
   103  	x := r.PathPrefix("/apps").Subrouter()
   104  	x.Methods(http.MethodGet).HandlerFunc(h.GetApps)
   105  	x.Methods(http.MethodDelete).Handler(authorizer.RequireAdminRole(http.HandlerFunc(h.DeleteApp)))
   106  }