-
Notifications
You must be signed in to change notification settings - Fork 10
/
httpserver.go
81 lines (64 loc) · 2.11 KB
/
httpserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"github.com/gorilla/mux"
"html/template"
"net/http"
"time"
)
const (
// Route vars are gorilla/mux paths variables.
FileRouteVar = "file"
TemplateRouteVar = "template"
TypeRouteVar = "type"
WebAssetsDir = "./assets"
TemplatesDir = "./templates"
)
func StartHTTPServer(port int) chan error {
r := mux.NewRouter()
// This is the asset sub-router. It routes the "/assets" path prefix.
// Assets are found in sub-directories under /assets (i.e. css, js...)
assetsRouter := r.PathPrefix("/assets").Methods("GET").Subrouter()
assetsRouter.Handle("/{"+TypeRouteVar+"}/{"+FileRouteVar+"}", http.StripPrefix("/assets/", http.FileServer(http.Dir(WebAssetsDir))))
thandler := NewTemplateHandler()
r.Handle("/", thandler)
r.Handle("/{"+TemplateRouteVar+"}", thandler)
http.Handle("/", r)
done := make(chan error)
go withLogging(func() {
Log.WithField("port", port).Info("HTTP listen and serve.")
done <- http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
})
return done
}
type TemplateArgs struct {
}
type TemplateHandler struct {
baseTemplate *template.Template
}
func newTemplateFuncMap() template.FuncMap {
return template.FuncMap{
"timeNow": time.Now,
"localHostname": GetLocalHostname,
"getInterfaces": getInterfaces,
"getInterfaceIPAddressesString": getInterfaceIPAddressesString,
"getCPUInfo": getCPUInfo,
"getNetworkDeviceStats": getNetworkDeviceStats,
"getInterfaceRateStats": getInterfaceRateStats,
}
}
func NewTemplateHandler() *TemplateHandler {
funcs := newTemplateFuncMap()
return &TemplateHandler{baseTemplate: template.Must(template.New("base").Funcs(funcs).ParseGlob(TemplatesDir + "/*.html"))}
}
func (handler *TemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
template := vars[TemplateRouteVar]
if template == "" {
template = "interfaces.html"
}
args := &TemplateArgs{}
if err := handler.baseTemplate.ExecuteTemplate(w, template, args); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}