From e2085bf417b84fa87c579d907818a4ccca19e6e5 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 24 Jan 2016 20:14:03 +0100 Subject: [PATCH] Add support for variable root mount points. That is, make it possible to serve the webUI from a different path than /. --- main.go | 5 ++++- server/error.go | 2 +- server/server.go | 42 +++++++++++++++++++++++++++++++++++------ templates/graphs.tmpl | 4 ++-- templates/host.tmpl | 4 ++-- templates/hosts.tmpl | 2 +- templates/main.tmpl | 16 ++++++++-------- templates/metric.tmpl | 6 +++--- templates/metrics.tmpl | 4 ++-- templates/service.tmpl | 2 +- templates/services.tmpl | 4 ++-- 11 files changed, 62 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index 5ded814..699651c 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,8 @@ var ( listen = flag.String("listen", ":8080", "address to listen for incoming connections") tmpl = flag.String("template-path", "templates", "location of template files") static = flag.String("static-path", "static", "location of static files") + + root = flag.String("root", "/", "root mount point of the server") ) func init() { @@ -65,13 +67,14 @@ func main() { srv, err := server.New(*addr, *username, server.Config{ TemplatePath: *tmpl, StaticPath: *static, + Root: *root, }) if err != nil { fatalf("Failed to construct web-server: %v", err) } log.Printf("Listening on %s.", *listen) - http.Handle("/", srv) + http.Handle(*root, srv) err = http.ListenAndServe(*listen, nil) if err != nil { fatalf("Failed to set up HTTP server on address %q: %v", *listen, err) diff --git a/server/error.go b/server/error.go index 270225a..8509b3d 100644 --- a/server/error.go +++ b/server/error.go @@ -36,7 +36,7 @@ import ( ) func (s *Server) notfound(w http.ResponseWriter, r *http.Request) { - s.err(w, http.StatusNotFound, fmt.Errorf("%s not found", r.RequestURI)) + s.err(w, http.StatusNotFound, fmt.Errorf("%s not found", r.URL.Path)) } func (s *Server) badrequest(w http.ResponseWriter, err error) { diff --git a/server/server.go b/server/server.go index 1f5ea94..fb5ba22 100644 --- a/server/server.go +++ b/server/server.go @@ -47,6 +47,9 @@ type Config struct { // StaticPath specifies the relative or absolute location of static files. StaticPath string + + // Root mount point of the server. + Root string } // A Server implements an http.Handler that serves the SysDB user interface. @@ -62,11 +65,21 @@ type Server struct { // Base directory of static files. basedir string + + // Root mount point. + root string } // New constructs a new SysDB web server using the specified configuration. func New(addr, user string, cfg Config) (*Server, error) { - s := &Server{results: make(map[string]*template.Template)} + s := &Server{ + results: make(map[string]*template.Template), + basedir: cfg.StaticPath, + root: cfg.Root, + } + if s.root == "" { + s.root = "/" + } var err error if s.c, err = client.Connect(addr, user); err != nil { @@ -76,18 +89,17 @@ func New(addr, user string, cfg Config) (*Server, error) { log.Printf("Connected to SysDB %d.%d.%d%s.", major, minor, patch, extra) } - if s.main, err = cfg.parse("main.tmpl"); err != nil { + if s.main, err = cfg.parse(s, "main.tmpl"); err != nil { return nil, err } types := []string{"graphs", "host", "hosts", "service", "services", "metric", "metrics"} for _, t := range types { - s.results[t], err = cfg.parse(t + ".tmpl") + s.results[t], err = cfg.parse(s, t+".tmpl") if err != nil { return nil, err } } - s.basedir = cfg.StaticPath s.mux = map[string]handler{ "images": s.static, "style": s.static, @@ -96,8 +108,10 @@ func New(addr, user string, cfg Config) (*Server, error) { return s, nil } -func (cfg Config) parse(name string) (*template.Template, error) { - t := template.New(filepath.Base(name)) +func (cfg Config) parse(s *Server, name string) (*template.Template, error) { + t := template.New(filepath.Base(name)).Funcs(template.FuncMap{ + "root": s.Root, + }) return t.ParseFiles(filepath.Join(cfg.TemplatePath, name)) } @@ -134,6 +148,13 @@ var content = map[string]func(request, *Server) (*page, error){ // the SysDB user interface. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { path := r.RequestURI + if !strings.HasPrefix(path, s.root) { + s.notfound(w, r) + return + } + path = strings.TrimPrefix(path, s.root) + r.URL.Path = strings.TrimPrefix(r.URL.Path, s.root) + if len(path) > 0 && path[0] == '/' { path = path[1:] } @@ -205,6 +226,15 @@ func (s *Server) static(w http.ResponseWriter, req request) { http.ServeFile(w, req.r, filepath.Clean(filepath.Join(s.basedir, req.r.URL.Path))) } +// Root returns the root mount point of the server suitable for use as a path +// prefix. +func (s *Server) Root() string { + if s.root[len(s.root)-1] == '/' { + return s.root + } + return s.root + "/" +} + func index(_ request, s *Server) (*page, error) { major, minor, patch, extra, err := s.c.ServerVersion() if err != nil { diff --git a/templates/graphs.tmpl b/templates/graphs.tmpl index 5c8b490..221273a 100644 --- a/templates/graphs.tmpl +++ b/templates/graphs.tmpl @@ -1,6 +1,6 @@

Graphs

-
+

@@ -13,7 +13,7 @@ {{end}}

{{if .Metrics}} - + {{end}}

 

diff --git a/templates/host.tmpl b/templates/host.tmpl index 6193070..4f1ea1f 100644 --- a/templates/host.tmpl +++ b/templates/host.tmpl @@ -15,7 +15,7 @@ {{if len .Services}} Services {{range .Services}} - {{.Name}} + {{.Name}} {{end}} {{else}} No services @@ -23,7 +23,7 @@ {{if len .Metrics}} Metrics {{range .Metrics}} - {{.Name}} + {{.Name}} {{end}} {{else}} No Metrics diff --git a/templates/hosts.tmpl b/templates/hosts.tmpl index 4eabaef..aff7aa8 100644 --- a/templates/hosts.tmpl +++ b/templates/hosts.tmpl @@ -4,7 +4,7 @@ {{range .}} - + {{end}}
HostLast update
{{.Name}}{{.LastUpdate}}
{{.Name}}{{.LastUpdate}}
{{else}} diff --git a/templates/main.tmpl b/templates/main.tmpl index e6049eb..46575f5 100644 --- a/templates/main.tmpl +++ b/templates/main.tmpl @@ -10,8 +10,8 @@ - - + + @@ -21,11 +21,11 @@