-
Notifications
You must be signed in to change notification settings - Fork 45
/
server.go
64 lines (53 loc) · 1.29 KB
/
server.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
package main
import (
//"fmt"
"net/http"
"text/template"
"strconv"
"log"
)
var (
ChartHandlers = make(map[string]ChartIf)
ChartFiles []string
Index int
)
func handler(w http.ResponseWriter, r *http.Request) {
values := r.URL.Query()
i := values.Get("index")
if len(i) > 0 {
Index, _ = strconv.Atoi(i)
} else {
Index++
}
Index = Index % len(ChartFiles)
log.Printf("Index=%v rendering %v", Index, ChartFiles[Index])
tt, err := Parse(ChartFiles[Index])
if err != nil {
w.Write([]byte(err.Error()))
return
}
if t, err := template.New("foo").Parse(tt.tmpl); err != nil {
w.Write([]byte(err.Error()))
} else {
if err = t.ExecuteTemplate(w, "T", tt.args); err != nil {
w.Write([]byte(err.Error()))
}
}
}
func ListenAndServe(addr string) error {
http.HandleFunc("/", handler)
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {})
var err error
ChartFiles, err = LookupChartFiles(".")
if err != nil {
return err
}
// Register chart handlders
ChartHandlers["spline"] = new(SplineChart)
ChartHandlers["column"] = new(SplineChart)
ChartHandlers["area"] = new(SplineChart)
ChartHandlers["bar"] = new(SplineChart)
ChartHandlers["line"] = new(SplineChart)
ChartHandlers["pie"] = new(PieChart)
return http.ListenAndServe(addr, nil)
}