...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ui
15
16 import (
17 "fmt"
18 "net/http"
19 _ "net/http/pprof"
20 "path"
21
22 "github.com/go-kit/log"
23 "github.com/prometheus/client_golang/prometheus/promhttp"
24 "github.com/prometheus/common/route"
25
26 "github.com/prometheus/alertmanager/asset"
27 )
28
29
30 func Register(r *route.Router, reloadCh chan<- chan error, logger log.Logger) {
31 r.Get("/metrics", promhttp.Handler().ServeHTTP)
32
33 r.Get("/", func(w http.ResponseWriter, req *http.Request) {
34 disableCaching(w)
35
36 req.URL.Path = "/static/"
37 fs := http.FileServer(asset.Assets)
38 fs.ServeHTTP(w, req)
39 })
40
41 r.Get("/script.js", func(w http.ResponseWriter, req *http.Request) {
42 disableCaching(w)
43
44 req.URL.Path = "/static/script.js"
45 fs := http.FileServer(asset.Assets)
46 fs.ServeHTTP(w, req)
47 })
48
49 r.Get("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {
50 disableCaching(w)
51
52 req.URL.Path = "/static/favicon.ico"
53 fs := http.FileServer(asset.Assets)
54 fs.ServeHTTP(w, req)
55 })
56
57 r.Get("/lib/*path", func(w http.ResponseWriter, req *http.Request) {
58 disableCaching(w)
59
60 req.URL.Path = path.Join("/static/lib", route.Param(req.Context(), "path"))
61 fs := http.FileServer(asset.Assets)
62 fs.ServeHTTP(w, req)
63 })
64
65 r.Post("/-/reload", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
66 errc := make(chan error)
67 defer close(errc)
68
69 reloadCh <- errc
70 if err := <-errc; err != nil {
71 http.Error(w, fmt.Sprintf("failed to reload config: %s", err), http.StatusInternalServerError)
72 }
73 }))
74
75 r.Get("/-/healthy", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
76 w.WriteHeader(http.StatusOK)
77 fmt.Fprintf(w, "OK")
78 }))
79 r.Head("/-/healthy", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
80 w.WriteHeader(http.StatusOK)
81 }))
82 r.Get("/-/ready", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
83 w.WriteHeader(http.StatusOK)
84 fmt.Fprintf(w, "OK")
85 }))
86 r.Head("/-/ready", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
87 w.WriteHeader(http.StatusOK)
88 }))
89
90 r.Get("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
91 r.Post("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
92 }
93
94 func disableCaching(w http.ResponseWriter) {
95 w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
96 w.Header().Set("Pragma", "no-cache")
97 w.Header().Set("Expires", "0")
98 }
99
View as plain text