2023-04-25 00:18:44 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2023-12-13 04:43:11 +08:00
|
|
|
"net/http"
|
2023-12-12 03:20:48 +08:00
|
|
|
"net/url"
|
|
|
|
|
|
2023-12-13 04:43:11 +08:00
|
|
|
"github.com/gorilla/mux"
|
2023-04-25 00:18:44 +08:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RequestHandlerFunc func(*contextmodel.ReqContext) response.Response
|
|
|
|
|
|
|
|
|
|
type Hooks struct {
|
2023-12-13 04:43:11 +08:00
|
|
|
logger log.Logger
|
|
|
|
|
router *mux.Router
|
|
|
|
|
routeHooks map[*mux.Route]RequestHandlerFunc
|
2023-04-25 00:18:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewHooks creates an empty set of request handler hooks. Hooks can be used
|
|
|
|
|
// to replace handlers for specific paths.
|
|
|
|
|
func NewHooks(logger log.Logger) *Hooks {
|
|
|
|
|
return &Hooks{
|
2023-12-13 04:43:11 +08:00
|
|
|
logger: logger,
|
|
|
|
|
router: mux.NewRouter(),
|
|
|
|
|
routeHooks: make(map[*mux.Route]RequestHandlerFunc),
|
2023-04-25 00:18:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add creates a new request hook for a path, causing requests to the path to
|
|
|
|
|
// be handled by the hook function, and not the original handler.
|
2023-12-13 04:43:11 +08:00
|
|
|
func (h *Hooks) Set(method string, path string, hook RequestHandlerFunc) {
|
2023-09-05 00:46:34 +08:00
|
|
|
h.logger.Info("Setting hook override for the specified route", "path", path)
|
2023-12-13 04:43:11 +08:00
|
|
|
route := h.router.NewRoute().Path(path).Methods(method)
|
|
|
|
|
h.routeHooks[route] = hook
|
2023-04-25 00:18:44 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-12 03:20:48 +08:00
|
|
|
// Get returns a hook if one is defined for the matching URL.
|
|
|
|
|
// Get also returns a bool indicating whether or not a matching hook exists.
|
2023-12-13 04:43:11 +08:00
|
|
|
func (h *Hooks) Get(method string, url *url.URL) (RequestHandlerFunc, bool) {
|
|
|
|
|
req := http.Request{Method: method, URL: url}
|
|
|
|
|
|
|
|
|
|
match := mux.RouteMatch{}
|
|
|
|
|
if ok := h.router.Match(&req, &match); ok {
|
|
|
|
|
return h.routeHooks[match.Route], ok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, false
|
2023-12-12 03:20:48 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-25 00:18:44 +08:00
|
|
|
// Wrap returns a new handler which will intercept paths with hooks configured,
|
|
|
|
|
// and invoke the hooked in handler instead. If no hook is configured for a path,
|
|
|
|
|
// then the given handler is invoked.
|
|
|
|
|
func (h *Hooks) Wrap(next RequestHandlerFunc) RequestHandlerFunc {
|
|
|
|
|
return func(req *contextmodel.ReqContext) response.Response {
|
2025-04-10 20:42:23 +08:00
|
|
|
if hook, ok := h.Get(req.Req.Method, req.Req.URL); ok {
|
|
|
|
|
h.logger.Debug("Hook defined - invoking new handler", "path", req.Req.URL.Path)
|
2023-04-25 00:18:44 +08:00
|
|
|
return hook(req)
|
|
|
|
|
}
|
|
|
|
|
return next(req)
|
|
|
|
|
}
|
|
|
|
|
}
|