Skip to content

Commit

Permalink
add no-caching headers
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Mar 29, 2020
1 parent bd83843 commit b75cf1a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cmd/alertmanager/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func newRestApi(ctx context.Context) http.Handler {
mux := httputils.NewMethodMux()

mux.GET.HandleFunc("/alerts", func(w http.ResponseWriter, r *http.Request) {
noCacheHeaders(w)

handleJsonOutput(w, app.State.ActiveAlerts())
})

Expand Down Expand Up @@ -57,6 +59,8 @@ func newRestApi(ctx context.Context) http.Handler {
mux.GET.HandleFunc("/alerts/acknowledge", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")

noCacheHeaders(w)

if err := alertAck(r.Context(), id); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -66,13 +70,17 @@ func newRestApi(ctx context.Context) http.Handler {
})

mux.GET.HandleFunc("/deadmansswitches", func(w http.ResponseWriter, r *http.Request) {
noCacheHeaders(w)

handleJsonOutput(w, app.State.DeadMansSwitches())
})

// /deadmansswitch/checkin?subject=ubackup_done&ttl=24h30m
mux.GET.HandleFunc("/deadmansswitch/checkin", func(w http.ResponseWriter, r *http.Request) {
// same semantic hack here as acknowledge endpoint

noCacheHeaders(w)

// handles validation
handleDeadMansSwitchCheckin(w, r, alertmanagertypes.DeadMansSwitchCheckinRequest{
Subject: r.URL.Query().Get("subject"),
Expand Down Expand Up @@ -173,3 +181,7 @@ func runStandaloneRestApi(ctx context.Context, logger *log.Logger) error {
func ackLink(alert amstate.Alert) string {
return os.Getenv("API_ENDPOINT") + "/alerts/acknowledge?id=" + alert.Id
}

func noCacheHeaders(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "no-store, must-revalidate")
}

0 comments on commit b75cf1a

Please sign in to comment.