Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve web redirection and 404 behavior. #67

Merged
merged 1 commit into from
May 23, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 14 additions & 11 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"html/template"
"net/http"
_ "net/http/pprof"
"strings"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -42,35 +43,37 @@ type WebService struct {

func (w WebService) ServeForever(pathPrefix string) error {

http.Handle(pathPrefix + "favicon.ico", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Handle(pathPrefix+"favicon.ico", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", 404)
}))


http.HandleFunc("/", prometheus.InstrumentHandlerFunc("index", func(rw http.ResponseWriter, req *http.Request) {
// The "/" pattern matches everything, so we need to check
// that we're at the root here.
if req.URL.Path == pathPrefix {
w.AlertsHandler.ServeHTTP(rw, req)
} else if req.URL.Path == "/" {
// We're running under a prefix but the user requested "/".
} else if req.URL.Path == strings.TrimRight(pathPrefix, "/") {
http.Redirect(rw, req, pathPrefix, http.StatusFound)
} else if !strings.HasPrefix(req.URL.Path, pathPrefix) {
// We're running under a prefix but the user requested something
// outside of it. Let's see if this page exists under the prefix.
http.Redirect(rw, req, pathPrefix+strings.TrimLeft(req.URL.Path, "/"), http.StatusFound)
} else {
http.NotFound(rw, req)
}
}))

http.Handle(pathPrefix + "alerts", prometheus.InstrumentHandler("alerts", w.AlertsHandler))
http.Handle(pathPrefix + "silences", prometheus.InstrumentHandler("silences", w.SilencesHandler))
http.Handle(pathPrefix + "status", prometheus.InstrumentHandler("status", w.StatusHandler))
http.Handle(pathPrefix+"alerts", prometheus.InstrumentHandler("alerts", w.AlertsHandler))
http.Handle(pathPrefix+"silences", prometheus.InstrumentHandler("silences", w.SilencesHandler))
http.Handle(pathPrefix+"status", prometheus.InstrumentHandler("status", w.StatusHandler))

http.Handle(pathPrefix + "metrics", prometheus.Handler())
http.Handle(pathPrefix+"metrics", prometheus.Handler())
if *useLocalAssets {
http.Handle(pathPrefix + "static/", http.StripPrefix(pathPrefix + "static/", http.FileServer(http.Dir("web/static"))))
http.Handle(pathPrefix+"static/", http.StripPrefix(pathPrefix+"static/", http.FileServer(http.Dir("web/static"))))
} else {
http.Handle(pathPrefix + "static/", http.StripPrefix(pathPrefix + "static/", new(blob.Handler)))
http.Handle(pathPrefix+"static/", http.StripPrefix(pathPrefix+"static/", new(blob.Handler)))
}
http.Handle(pathPrefix + "api/", w.AlertManagerService.Handler())
http.Handle(pathPrefix+"api/", w.AlertManagerService.Handler())

glog.Info("listening on ", *listenAddress)

Expand Down