github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/api/applications.go (about) 1 package api 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 9 "github.com/pyroscope-io/pyroscope/pkg/model/appmetadata" 10 "github.com/pyroscope-io/pyroscope/pkg/server/httputils" 11 ) 12 13 type ApplicationListerAndDeleter interface { 14 List(context.Context) ([]appmetadata.ApplicationMetadata, error) 15 Delete(ctx context.Context, name string) error 16 } 17 18 type ApplicationsHandler struct { 19 svc ApplicationListerAndDeleter 20 httpUtils httputils.Utils 21 } 22 23 func NewApplicationsHandler(svc ApplicationListerAndDeleter, httpUtils httputils.Utils) *ApplicationsHandler { 24 return &ApplicationsHandler{ 25 svc: svc, 26 httpUtils: httpUtils, 27 } 28 } 29 30 func (h *ApplicationsHandler) GetApps(w http.ResponseWriter, r *http.Request) { 31 apps, err := h.svc.List(r.Context()) 32 if err != nil { 33 h.httpUtils.HandleError(r, w, err) 34 return 35 } 36 37 w.WriteHeader(http.StatusOK) 38 h.httpUtils.WriteResponseJSON(r, w, apps) 39 } 40 41 type DeleteAppInput struct { 42 Name string `json:"name"` 43 } 44 45 func (h *ApplicationsHandler) DeleteApp(w http.ResponseWriter, r *http.Request) { 46 var payload DeleteAppInput 47 48 fmt.Println("calling delete app") 49 err := json.NewDecoder(r.Body).Decode(&payload) 50 if err != nil { 51 h.httpUtils.HandleError(r, w, httputils.JSONError{Err: err}) 52 return 53 } 54 55 err = h.svc.Delete(r.Context(), payload.Name) 56 if err != nil { 57 h.httpUtils.HandleError(r, w, err) 58 return 59 } 60 }