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

     1  package server
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/sirupsen/logrus"
     7  
     8  	"github.com/pyroscope-io/pyroscope/pkg/history"
     9  	"github.com/pyroscope-io/pyroscope/pkg/server/httputils"
    10  )
    11  
    12  type HistoryHandler struct {
    13  	log        *logrus.Logger
    14  	httpUtils  httputils.Utils
    15  	historyMgr history.Manager
    16  }
    17  
    18  func (ctrl *Controller) historyHandler() http.HandlerFunc {
    19  	return NewHistoryHandler(ctrl.log, ctrl.httpUtils, ctrl.historyMgr).ServeHTTP
    20  }
    21  
    22  func NewHistoryHandler(
    23  	l *logrus.Logger,
    24  	httpUtils httputils.Utils,
    25  	historyMgr history.Manager,
    26  ) *HistoryHandler {
    27  	return &HistoryHandler{
    28  		log:        l,
    29  		httpUtils:  httpUtils,
    30  		historyMgr: historyMgr,
    31  	}
    32  }
    33  
    34  type response struct {
    35  	Next    string           `json:"next"`
    36  	History []*history.Entry `json:"history"`
    37  }
    38  
    39  func (rh *HistoryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    40  	res, next, err := rh.historyMgr.List(r.Context(), r.URL.Query().Get("cursor"))
    41  	if err != nil {
    42  		rh.httpUtils.HandleError(r, w, err)
    43  		return
    44  	}
    45  	rh.httpUtils.MustJSON(r, w, &response{
    46  		Next:    next,
    47  		History: res,
    48  	})
    49  }