Code

Added support for simple SVG graphs for metrics.
[sysdb/webui.git] / server / server.go
index 706dac2492a86ab2882b1714d991571fef9f4094..59c1e4215a468bea1a3a36813eafc97014bfac99 100644 (file)
@@ -61,12 +61,15 @@ type Config struct {
 type Server struct {
        conns chan *client.Conn
 
+       // Request multiplexer
+       mux map[string]handler
+
        // Templates:
        main    *template.Template
        results map[string]*template.Template
 
-       // Static content:
-       static http.Handler
+       // Base directory of static files.
+       basedir string
 }
 
 // New constructs a new SysDB web server using the specified configuration.
@@ -97,7 +100,12 @@ func New(cfg Config) (*Server, error) {
                }
        }
 
-       s.static = http.FileServer(http.Dir(cfg.StaticPath))
+       s.basedir = cfg.StaticPath
+       s.mux = map[string]handler{
+               "images": s.static,
+               "style":  s.static,
+               "graph":  s.graph,
+       }
        return s, nil
 }
 
@@ -112,7 +120,10 @@ type request struct {
        args []string
 }
 
-var handlers = map[string]func(request, *Server) (template.HTML, error){
+type handler func(http.ResponseWriter, request)
+
+// Content generators for HTML pages.
+var content = map[string]func(request, *Server) (template.HTML, error){
        "": index,
 
        // Queries
@@ -134,11 +145,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        }
        fields := strings.Split(path, "/")
 
-       if fields[0] == "style" || fields[0] == "images" {
-               s.static.ServeHTTP(w, r)
-               return
-       }
-
        req := request{
                r:   r,
                cmd: fields[0],
@@ -153,7 +159,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
                }
        }
 
-       f, ok := handlers[req.cmd]
+       if h := s.mux[fields[0]]; h != nil {
+               h(w, req)
+               return
+       }
+
+       f, ok := content[req.cmd]
        if !ok {
                s.notfound(w, r)
                return
@@ -186,6 +197,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        io.Copy(w, &buf)
 }
 
+// static serves static content.
+func (s *Server) static(w http.ResponseWriter, req request) {
+       http.ServeFile(w, req.r, filepath.Clean(filepath.Join(s.basedir, req.r.URL.Path)))
+}
+
 // Content handlers.
 
 func index(_ request, s *Server) (template.HTML, error) {
@@ -302,6 +318,10 @@ func (s *Server) query(cmd string) (interface{}, error) {
                        var host sysdb.Host
                        err = proto.Unmarshal(m, &host)
                        res = host
+               case proto.Timeseries:
+                       var ts sysdb.Timeseries
+                       err = proto.Unmarshal(m, &ts)
+                       res = ts
                default:
                        return nil, fmt.Errorf("Unsupported data type %d", t)
                }