diff --git a/server/server.go b/server/server.go
index 706dac2492a86ab2882b1714d991571fef9f4094..a8e34d9c32a063c5d183070e4cb48e258e5250de 100644 (file)
--- a/server/server.go
+++ b/server/server.go
"io"
"log"
"net/http"
+ "net/url"
"path/filepath"
"strings"
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.
}
}
- 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
}
args []string
}
-var handlers = map[string]func(request, *Server) (template.HTML, error){
+type handler func(http.ResponseWriter, request)
+
+type page struct {
+ Title string
+ Query string
+ Content template.HTML
+}
+
+// Content generators for HTML pages.
+var content = map[string]func(request, *Server) (*page, error){
"": index,
// Queries
// ServeHTTP implements the http.Handler interface and serves
// the SysDB user interface.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- path := r.URL.Path
+ path := r.RequestURI
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
- fields := strings.Split(path, "/")
-
- if fields[0] == "style" || fields[0] == "images" {
- s.static.ServeHTTP(w, r)
- return
+ var fields []string
+ for _, f := range strings.Split(path, "/") {
+ f, err := url.QueryUnescape(f)
+ if err != nil {
+ s.err(w, http.StatusBadRequest, fmt.Errorf("Error: %v", err))
+ return
+ }
+ fields = append(fields, f)
}
req := 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
}
r.ParseForm()
- content, err := f(req, s)
+ page, err := f(req, s)
if err != nil {
s.err(w, http.StatusBadRequest, fmt.Errorf("Error: %v", err))
return
}
- page := struct {
- Title string
- Query string
- Content template.HTML
- }{
- Title: "SysDB - The System Database",
- Query: r.FormValue("query"),
- Content: content,
+ page.Query = r.FormValue("query")
+ if page.Title == "" {
+ page.Title = "SysDB - The System Database"
}
var buf bytes.Buffer
- err = s.main.Execute(&buf, &page)
+ err = s.main.Execute(&buf, page)
if err != nil {
s.internal(w, err)
return
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) {
- return "<section><h1>Welcome to the System Database.</h1></section>", nil
+func index(_ request, s *Server) (*page, error) {
+ return &page{Content: "<section><h1>Welcome to the System Database.</h1></section>"}, nil
}
-func listAll(req request, s *Server) (template.HTML, error) {
+func listAll(req request, s *Server) (*page, error) {
if len(req.args) != 0 {
- return "", fmt.Errorf("%s not found", strings.Title(req.cmd))
+ return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
}
res, err := s.query(fmt.Sprintf("LIST %s", req.cmd))
if err != nil {
- return "", err
+ return nil, err
}
// the template *must* exist
return tmpl(s.results[req.cmd], res)
}
-func lookup(req request, s *Server) (template.HTML, error) {
+func lookup(req request, s *Server) (*page, error) {
if req.r.Method != "POST" {
- return "", errors.New("Method not allowed")
+ return nil, errors.New("Method not allowed")
}
q := proto.EscapeString(req.r.FormValue("query"))
if q == "''" {
- return "", errors.New("Empty query")
+ return nil, errors.New("Empty query")
}
res, err := s.query(fmt.Sprintf("LOOKUP hosts MATCHING name =~ %s", q))
if err != nil {
- return "", err
+ return nil, err
}
return tmpl(s.results["hosts"], res)
}
-func fetch(req request, s *Server) (template.HTML, error) {
+func fetch(req request, s *Server) (*page, error) {
if len(req.args) == 0 {
- return "", fmt.Errorf("%s not found", strings.Title(req.cmd))
+ return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
}
var q string
switch req.cmd {
case "host":
if len(req.args) != 1 {
- return "", fmt.Errorf("%s not found", strings.Title(req.cmd))
+ return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
}
q = fmt.Sprintf("FETCH host %s", proto.EscapeString(req.args[0]))
case "service", "metric":
- if len(req.args) < 2 {
- return "", fmt.Errorf("%s not found", strings.Title(req.cmd))
+ if len(req.args) != 2 {
+ return nil, fmt.Errorf("%s not found", strings.Title(req.cmd))
}
host := proto.EscapeString(req.args[0])
- name := proto.EscapeString(strings.Join(req.args[1:], "/"))
+ name := proto.EscapeString(req.args[1])
q = fmt.Sprintf("FETCH %s %s.%s", req.cmd, host, name)
default:
panic("Unknown request: fetch(" + req.cmd + ")")
res, err := s.query(q)
if err != nil {
- return "", err
+ return nil, err
}
return tmpl(s.results[req.cmd], res)
}
-func tmpl(t *template.Template, data interface{}) (template.HTML, error) {
+func tmpl(t *template.Template, data interface{}) (*page, error) {
var buf bytes.Buffer
if err := t.Execute(&buf, data); err != nil {
- return "", fmt.Errorf("Template error: %v", err)
+ return nil, fmt.Errorf("Template error: %v", err)
}
- return template.HTML(buf.String()), nil
+ return &page{Content: template.HTML(buf.String())}, nil
}
func html(s string) template.HTML {
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)
}