mirror of
https://github.com/prometheus/alertmanager
synced 2025-02-09 15:07:26 +00:00
allow CORS for api GET and OPTIONS
This commit is contained in:
parent
4ac976c7e6
commit
9d766e1843
23
api/api.go
23
api/api.go
@ -54,6 +54,20 @@ func init() {
|
|||||||
prometheus.Register(numInvalidAlerts)
|
prometheus.Register(numInvalidAlerts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var corsHeaders = map[string]string{
|
||||||
|
"Access-Control-Allow-Headers": "Accept, Authorization, Content-Type, Origin",
|
||||||
|
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
"Access-Control-Expose-Headers": "Date",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enables cross-site script calls.
|
||||||
|
func setCORS(w http.ResponseWriter) {
|
||||||
|
for h, v := range corsHeaders {
|
||||||
|
w.Header().Set(h, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// API provides registration of handlers for API routes.
|
// API provides registration of handlers for API routes.
|
||||||
type API struct {
|
type API struct {
|
||||||
alerts provider.Alerts
|
alerts provider.Alerts
|
||||||
@ -83,7 +97,14 @@ func New(alerts provider.Alerts, silences *silence.Silences, gf func() dispatch.
|
|||||||
// Register registers the API handlers under their correct routes
|
// Register registers the API handlers under their correct routes
|
||||||
// in the given router.
|
// in the given router.
|
||||||
func (api *API) Register(r *route.Router) {
|
func (api *API) Register(r *route.Router) {
|
||||||
ihf := prometheus.InstrumentHandlerFunc
|
ihf := func(name string, f http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return prometheus.InstrumentHandlerFunc(name, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
setCORS(w)
|
||||||
|
f(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Options("/*path", ihf("options", func(w http.ResponseWriter, r *http.Request) {}))
|
||||||
|
|
||||||
// Register legacy forwarder for alert pushing.
|
// Register legacy forwarder for alert pushing.
|
||||||
r.Post("/alerts", ihf("legacy_add_alerts", api.legacyAddAlerts))
|
r.Post("/alerts", ihf("legacy_add_alerts", api.legacyAddAlerts))
|
||||||
|
Loading…
Reference in New Issue
Block a user