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

     1  package admin
     2  
     3  //revive:disable:max-public-structs dependencies
     4  
     5  import (
     6  	"context"
     7  	"encoding/json"
     8  	"fmt"
     9  	"net/http"
    10  
    11  	"github.com/pyroscope-io/pyroscope/pkg/model/appmetadata"
    12  	"github.com/pyroscope-io/pyroscope/pkg/server/httputils"
    13  
    14  	"github.com/gorilla/mux"
    15  	"github.com/sirupsen/logrus"
    16  
    17  	"github.com/pyroscope-io/pyroscope/pkg/model"
    18  )
    19  
    20  type Controller struct {
    21  	log            *logrus.Logger
    22  	httpUtils      httputils.Utils
    23  	appService     ApplicationListerAndDeleter
    24  	userService    UserService
    25  	storageService StorageService
    26  }
    27  
    28  type UserService interface {
    29  	UpdateUserByName(ctx context.Context, name string, params model.UpdateUserParams) (model.User, error)
    30  }
    31  
    32  type StorageService interface {
    33  	Cleanup(ctx context.Context) error
    34  }
    35  
    36  type ApplicationListerAndDeleter interface {
    37  	List(ctx context.Context) (apps []appmetadata.ApplicationMetadata, err error)
    38  	Delete(ctx context.Context, name string) error
    39  }
    40  
    41  func NewController(
    42  	log *logrus.Logger,
    43  	appService ApplicationListerAndDeleter,
    44  	userService UserService,
    45  	storageService StorageService) *Controller {
    46  	return &Controller{
    47  		log: log,
    48  
    49  		appService:     appService,
    50  		userService:    userService,
    51  		storageService: storageService,
    52  	}
    53  }
    54  
    55  type UpdateUserRequest struct {
    56  	Password   *string `json:"password"`
    57  	IsDisabled *bool   `json:"isDisabled"`
    58  }
    59  
    60  func (r *UpdateUserRequest) SetIsDisabled(v bool) { r.IsDisabled = &v }
    61  
    62  func (ctrl *Controller) UpdateUserHandler(w http.ResponseWriter, r *http.Request) {
    63  	var req UpdateUserRequest
    64  	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
    65  		ctrl.writeError(w, http.StatusBadRequest, err, "failed to unmarshal JSON")
    66  		return
    67  	}
    68  
    69  	params := model.UpdateUserParams{
    70  		Password:   req.Password,
    71  		IsDisabled: req.IsDisabled,
    72  	}
    73  
    74  	username := mux.Vars(r)["username"]
    75  	if _, err := ctrl.userService.UpdateUserByName(r.Context(), username, params); err != nil {
    76  		ctrl.writeError(w, http.StatusInternalServerError, err, fmt.Sprintf("can't update user %s", username))
    77  	}
    78  }
    79  
    80  func (ctrl *Controller) StorageCleanupHandler(w http.ResponseWriter, r *http.Request) {
    81  	if err := ctrl.storageService.Cleanup(r.Context()); err != nil {
    82  		ctrl.writeError(w, http.StatusInternalServerError, err, "failed to clean up storage")
    83  	}
    84  }