diff --git a/main.go b/main.go index 8a1861fd..e2b6348d 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ package main import ( "flag" "os" + "strings" "time" "github.com/golang/glog" @@ -30,11 +31,19 @@ var ( configFile = flag.String("config.file", "alertmanager.conf", "Alert Manager configuration file name.") silencesFile = flag.String("silences.file", "silences.json", "Silence storage file name.") minRefreshPeriod = flag.Duration("alerts.min-refresh-period", 5*time.Minute, "Minimum required alert refresh period before an alert is purged.") + pathPrefix = flag.String("web.path-prefix", "/", "Prefix for all web paths.") ) func main() { flag.Parse() + if !strings.HasPrefix(*pathPrefix, "/") { + *pathPrefix = "/" + *pathPrefix + } + if !strings.HasSuffix(*pathPrefix, "/") { + *pathPrefix = *pathPrefix + "/" + } + versionInfoTmpl.Execute(os.Stdout, BuildInfo) conf := config.MustLoadFromFile(*configFile) @@ -79,17 +88,19 @@ func main() { }) statusHandler := &web.StatusHandler{ - Config: conf.String(), - Flags: flags, - BuildInfo: BuildInfo, - Birth: time.Now(), + Config: conf.String(), + Flags: flags, + BuildInfo: BuildInfo, + Birth: time.Now(), + PathPrefix: *pathPrefix, } webService := &web.WebService{ // REST API Service. AlertManagerService: &api.AlertManagerService{ - Manager: alertManager, - Silencer: silencer, + Manager: alertManager, + Silencer: silencer, + PathPrefix: *pathPrefix, }, // Template-based page handlers. @@ -102,7 +113,7 @@ func main() { }, StatusHandler: statusHandler, } - go webService.ServeForever() + go webService.ServeForever(*pathPrefix) // React to configuration changes. watcher := config.NewFileWatcher(*configFile) diff --git a/web/alerts.go b/web/alerts.go index 4990271e..049975aa 100644 --- a/web/alerts.go +++ b/web/alerts.go @@ -27,6 +27,7 @@ type AlertStatus struct { type AlertsHandler struct { Manager manager.AlertManager IsSilencedInterrogator manager.IsSilencedInterrogator + PathPrefix string } func (h *AlertsHandler) silenceForAlert(a *manager.Alert) *manager.Silence { @@ -39,5 +40,5 @@ func (h *AlertsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { AlertAggregates: h.Manager.GetAll(nil), SilenceForAlert: h.silenceForAlert, } - executeTemplate(w, "alerts", alertStatus) + executeTemplate(w, "alerts", alertStatus, h.PathPrefix) } diff --git a/web/api/api.go b/web/api/api.go index 8d82c55a..ab3e1164 100644 --- a/web/api/api.go +++ b/web/api/api.go @@ -27,17 +27,17 @@ import ( type AlertManagerService struct { Manager manager.AlertManager Silencer *manager.Silencer + PathPrefix string } func (s AlertManagerService) Handler() http.Handler { r := httprouter.New() - - r.POST("/api/alerts", s.addAlerts) - r.GET("/api/silences", s.silenceSummary) - r.POST("/api/silences", s.addSilence) - r.GET("/api/silences/:id", s.getSilence) - r.POST("/api/silences/:id", s.updateSilence) - r.DELETE("/api/silences/:id", s.deleteSilence) + r.POST(s.PathPrefix + "api/alerts", s.addAlerts) + r.GET(s.PathPrefix + "api/silences", s.silenceSummary) + r.POST(s.PathPrefix + "api/silences", s.addSilence) + r.GET(s.PathPrefix + "api/silences/:id", s.getSilence) + r.POST(s.PathPrefix + "api/silences/:id", s.updateSilence) + r.DELETE(s.PathPrefix + "api/silences/:id", s.deleteSilence) return r } diff --git a/web/silences.go b/web/silences.go index 711732cf..7387e509 100644 --- a/web/silences.go +++ b/web/silences.go @@ -25,11 +25,12 @@ type SilenceStatus struct { type SilencesHandler struct { Silencer *manager.Silencer + PathPrefix string } func (h *SilencesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { silenceStatus := &SilenceStatus{ Silences: h.Silencer.SilenceSummary(), } - executeTemplate(w, "silences", silenceStatus) + executeTemplate(w, "silences", silenceStatus, h.PathPrefix) } diff --git a/web/status.go b/web/status.go index 8c939a32..8336e318 100644 --- a/web/status.go +++ b/web/status.go @@ -22,10 +22,11 @@ import ( type StatusHandler struct { mu sync.Mutex - BuildInfo map[string]string - Config string - Flags map[string]string - Birth time.Time + BuildInfo map[string]string + Config string + Flags map[string]string + Birth time.Time + PathPrefix string } func (h *StatusHandler) UpdateConfig(c string) { @@ -39,5 +40,5 @@ func (h *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.mu.Lock() defer h.mu.Unlock() - executeTemplate(w, "status", h) + executeTemplate(w, "status", h, h.PathPrefix) } diff --git a/web/templates/_base.html b/web/templates/_base.html index 37dfcba1..cb50a8e2 100644 --- a/web/templates/_base.html +++ b/web/templates/_base.html @@ -7,13 +7,13 @@ - - + + - - + + - + {{template "head" .}} @@ -26,9 +26,9 @@ {{define "alertsTabClass"}}{{end}} {{define "silencesTabClass"}}{{end}} {{define "statusTabClass"}}{{end}} -