grafana/pkg/services/store/http.go
2022-04-15 08:01:58 -04:00

74 lines
1.7 KiB
Go

package store
import (
"net/http"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/web"
)
// HTTPStorageService passes raw HTTP requests to a well typed storage service
type HTTPStorageService interface {
List(c *models.ReqContext) response.Response
Read(c *models.ReqContext) response.Response
Delete(c *models.ReqContext) response.Response
Upload(c *models.ReqContext) response.Response
}
type httpStorage struct {
store StorageService
}
func ProvideHTTPService(store StorageService) HTTPStorageService {
return &httpStorage{
store: store,
}
}
func (s *httpStorage) Upload(c *models.ReqContext) response.Response {
action := "Upload"
scope, path := getPathAndScope(c)
return response.JSON(http.StatusOK, map[string]string{
"action": action,
"scope": scope,
"path": path,
})
}
func (s *httpStorage) Read(c *models.ReqContext) response.Response {
action := "Read"
scope, path := getPathAndScope(c)
return response.JSON(http.StatusOK, map[string]string{
"action": action,
"scope": scope,
"path": path,
})
}
func (s *httpStorage) Delete(c *models.ReqContext) response.Response {
action := "Delete"
scope, path := getPathAndScope(c)
return response.JSON(http.StatusOK, map[string]string{
"action": action,
"scope": scope,
"path": path,
})
}
func (s *httpStorage) List(c *models.ReqContext) response.Response {
params := web.Params(c.Req)
path := params["*"]
frame, err := s.store.List(c.Req.Context(), c.SignedInUser, path)
if err != nil {
return response.Error(400, "error reading path", err)
}
if frame == nil {
return response.Error(404, "not found", nil)
}
return response.JSONStreaming(http.StatusOK, frame)
}