summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 0165ca4)
raw | patch | inline | side by side (parent: 0165ca4)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 21 Nov 2014 20:04:29 +0000 (21:04 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 21 Nov 2014 20:04:29 +0000 (21:04 +0100) |
Currently, this is used for serving static content.
server/server.go | patch | blob | history |
diff --git a/server/server.go b/server/server.go
index 706dac2492a86ab2882b1714d991571fef9f4094..33a2536f3b6fd491604cea59dbde8099460d91cc 100644 (file)
--- a/server/server.go
+++ b/server/server.go
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,
+ }
return s, nil
}
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
}
fields := strings.Split(path, "/")
- if fields[0] == "style" || fields[0] == "images" {
- s.static.ServeHTTP(w, r)
- return
- }
-
req := request{
r: r,
cmd: fields[0],
}
}
- 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
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) {