-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
58 lines (53 loc) · 1.37 KB
/
web.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
package main
import (
"github.com/wcharczuk/go-chart"
"go.uber.org/zap"
"html/template"
"net/http"
"time"
)
func serveVolumes() {
http.HandleFunc("/chart.png", drawChart)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("./html/index.html"))
mux.Lock()
defer mux.Unlock()
data := TemplateData{
message,
maxVolume,
avgVolume,
volumeThreshold,
peaks,
minutes,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
tmpl.Execute(w, data)
})
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./html/css/"))))
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./html/js/"))))
url := "localhost:8090"
logger.Info("serve results", zap.String("url", url))
logger.Fatal("fail to serve", zap.Error(http.ListenAndServe(url, nil)))
}
func drawChart(res http.ResponseWriter, req *http.Request) {
graph := chart.Chart{
XAxis: chart.XAxis{
Style: chart.StyleShow(),
ValueFormatter: hmsFormatter,
},
YAxis: chart.YAxis{
Style: chart.StyleShow(),
},
Series: []chart.Series{
serie,
},
}
res.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, res)
}
func hmsFormatter(v interface{}) string {
if typed, isTyped := v.(float64); isTyped {
return time.Unix(0, int64(typed)).Format("15:04:05")
}
return "x"
}