grafana/pkg/tsdb/cloudwatch/routes/middleware.go
Nathan Vērzemnieks b5f26560c2
Cloudwatch: Remove core imports from infra/log (#81543)
* Cloudwatch: use logging from plugin-sdk-go instead of infra/log
* Add a link to the TODO issue for moving `instrumentContext`
2024-02-07 13:53:05 +01:00

36 lines
1.2 KiB
Go

package routes
import (
"net/http"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
)
func ResourceRequestMiddleware(handleFunc models.RouteHandlerFunc, logger log.Logger, reqCtxFactory models.RequestContextFactoryFunc) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
respondWithError(rw, models.NewHttpError("Invalid method", http.StatusMethodNotAllowed, nil))
return
}
ctx := req.Context()
pluginContext := httpadapter.PluginConfigFromContext(ctx)
json, httpError := handleFunc(ctx, pluginContext, reqCtxFactory, req.URL.Query())
if httpError != nil {
logger.FromContext(ctx).Error("Error handling resource request", "error", httpError.Message)
respondWithError(rw, httpError)
return
}
rw.Header().Set("Content-Type", "application/json")
_, err := rw.Write(json)
if err != nil {
logger.FromContext(ctx).Error("Error handling resource request", "error", err)
respondWithError(rw, models.NewHttpError("error writing response in resource request middleware", http.StatusInternalServerError, err))
}
}
}