* Alerting: Allow hooking into request handler functions. Adds a facility to AlertNG for hooking into API handlers, allowing the replacement of request handlers for specific paths. One of goals of this approach was to allow hooking as late as possible in the request, e.g. after all middleware has been applied, to simplfiy usage. * Update pkg/services/ngalert/api/hooks.go Co-authored-by: gotjosh <josue.abreu@gmail.com> * Update pkg/services/ngalert/api/hooks.go Co-authored-by: gotjosh <josue.abreu@gmail.com> * Update pkg/services/ngalert/ngalert.go Co-authored-by: gotjosh <josue.abreu@gmail.com> * Fixes to review comments * Fix passing logger in --------- Co-authored-by: gotjosh <josue.abreu@gmail.com>
50 lines
1.7 KiB
Plaintext
50 lines
1.7 KiB
Plaintext
{{>partial_header}}
|
|
package {{packageName}}
|
|
|
|
{{#operations}}
|
|
import (
|
|
"github.com/go-macaron/binding"
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
|
)
|
|
|
|
type {{classname}} interface { {{#operation}}
|
|
{{nickname}}(*contextmodel.ReqContext) response.Response{{/operation}}
|
|
}
|
|
|
|
{{#operations}}{{#operation}}
|
|
func (f *{{classname}}Handler) {{nickname}}(ctx *contextmodel.ReqContext) response.Response { {{#hasPathParams}}
|
|
// Parse Path Parameters{{/hasPathParams}}{{#pathParams}}
|
|
{{paramName}}Param := web.Params(ctx.Req)[":{{baseName}}"]{{/pathParams}}
|
|
{{#bodyParams}}
|
|
// Parse Request Body
|
|
conf := apimodels.{{dataType}}{}
|
|
if err := web.Bind(ctx.Req, &conf); err != nil {
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
}
|
|
{{/bodyParams}}return f.handle{{nickname}}(ctx{{#bodyParams}}, conf{{/bodyParams}}{{#pathParams}}, {{paramName}}Param{{/pathParams}})
|
|
}
|
|
{{/operation}}{{/operations}}
|
|
|
|
func (api *API) Register{{classname}}Endpoints(srv {{classname}}, m *metrics.API) {
|
|
api.RouteRegister.Group("", func(group routing.RouteRegister){ {{#operations}}{{#operation}}
|
|
group.{{httpMethod}}(
|
|
toMacaronPath("{{{path}}}"),
|
|
api.authorize(http.Method{{httpMethod}}, "{{{path}}}"),
|
|
metrics.Instrument(
|
|
http.Method{{httpMethod}},
|
|
"{{{path}}}",
|
|
api.Hooks.Wrap(srv.{{nickname}}),
|
|
m,
|
|
),
|
|
){{/operation}}{{/operations}}
|
|
}, middleware.ReqSignedIn)
|
|
}{{#operation}}
|
|
{{/operation}}{{/operations}}
|