diff --git a/server/server.go b/server/server.go
index 6b3f8d07a57e5ae715fb828df75cb1f4c556332c..8d0d72dc191770f6810896306ea3f0f3beca64b5 100644 (file)
--- a/server/server.go
+++ b/server/server.go
import (
"bytes"
- "errors"
"fmt"
"html/template"
"io"
+ "log"
"net/http"
"net/url"
"path/filepath"
// A Config specifies configuration values for a SysDB web server.
type Config struct {
- // Conns is a slice of connections to a SysDB server instance. The number of
- // elements specifies the maximum number of parallel queries to the backend.
- // Note that a client connection is not thread-safe but multiple idle
- // connections don't impose any load on the server.
- Conns []*client.Conn
-
// TemplatePath specifies the relative or absolute location of template files.
TemplatePath string
// A Server implements an http.Handler that serves the SysDB user interface.
type Server struct {
- conns chan *client.Conn
+ c *client.Client
// Request multiplexer
mux map[string]handler
}
// New constructs a new SysDB web server using the specified configuration.
-func New(cfg Config) (*Server, error) {
- if len(cfg.Conns) == 0 {
- return nil, errors.New("need at least one client connection")
- }
+func New(addr, user string, cfg Config) (*Server, error) {
+ s := &Server{results: make(map[string]*template.Template)}
- s := &Server{
- conns: make(chan *client.Conn, len(cfg.Conns)),
- results: make(map[string]*template.Template),
+ var err error
+ if s.c, err = client.Connect(addr, user); err != nil {
+ return nil, err
}
- for _, c := range cfg.Conns {
- s.conns <- c
+ if major, minor, patch, extra, err := s.c.ServerVersion(); err == nil {
+ log.Printf("Connected to SysDB %d.%d.%d%s.", major, minor, patch, extra)
}
- var err error
- s.main, err = cfg.parse("main.tmpl")
- if err != nil {
+ if s.main, err = cfg.parse("main.tmpl"); err != nil {
return nil, err
}
-
types := []string{"host", "hosts", "service", "services", "metric", "metrics"}
for _, t := range types {
s.results[t], err = cfg.parse(t + ".tmpl")
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
+ if idx := strings.Index(path, "?"); idx != -1 {
+ path = path[:idx]
+ }
var fields []string
for _, f := range strings.Split(path, "/") {
f, err := url.QueryUnescape(f)
return
}
r.ParseForm()
- page, err := f(req, s)
+ p, err := f(req, s)
if err != nil {
- s.badrequest(w, fmt.Errorf("Error: %v", err))
- return
+ p = &page{
+ Content: "<section class=\"error\">" +
+ html(fmt.Sprintf("Error: %v", err)) +
+ "</section>",
+ }
}
- page.Query = r.FormValue("query")
- if page.Title == "" {
- page.Title = "SysDB - The System Database"
+ p.Query = r.FormValue("query")
+ if p.Title == "" {
+ p.Title = "SysDB - The System Database"
}
var buf bytes.Buffer
- err = s.main.Execute(&buf, page)
+ err = s.main.Execute(&buf, p)
if err != nil {
s.internal(w, err)
return
}
func index(_ request, s *Server) (*page, error) {
- return &page{Content: "<section><h1>Welcome to the System Database.</h1></section>"}, nil
+ major, minor, patch, extra, err := s.c.ServerVersion()
+ if err != nil {
+ return nil, err
+ }
+
+ content := fmt.Sprintf("<section>"+
+ "<h1>Welcome to the System Database.</h1>"+
+ "<p>Connected to SysDB %d.%d.%d%s</p>"+
+ "</section>", major, minor, patch, html(extra))
+ return &page{Content: template.HTML(content)}, nil
}
func tmpl(t *template.Template, data interface{}) (*page, error) {