-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
150 lines (123 loc) · 3.13 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"fmt"
"net/http"
"os"
"time"
"html/template"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/websocket"
)
var Now = time.Now
var upgrader = websocket.Upgrader{}
var (
interval = flag.Duration("i", 5*time.Second, "Polling interval: 5s, 1m")
port = flag.Int("p", 4444, "Dashboard HTTP port")
dashboard = flag.String("d", "", "Dashboard configuration file")
fs = flag.Bool("fs", false, "Serve static files from file system")
)
func main() {
flag.Usage = Usage
flag.Parse()
if *interval <= 0 {
fmt.Fprintln(os.Stderr, "Invalid polling interval.")
Usage()
os.Exit(1)
}
// Load configuration file
conf, err := LoadConf(*dashboard)
if err != nil {
fmt.Println("Could not read dashboard configuration:", err)
os.Exit(1)
}
// Start handler for web-socket connections
hub := NewHub()
go hub.Start()
fetcher := NewFetcher()
crawler := &Crawler{
interval: *interval,
fetcher: fetcher,
hub: hub,
widgets: conf.Widgets,
services: conf.Services,
}
go crawler.Start()
err = ListenAndServe(*port, hub, conf.Layout, *fs)
if err != nil {
fmt.Println("Could not start HTTP server:", err)
os.Exit(1)
}
}
func LoadTemplate(fsMode bool) (*template.Template, error) {
if fsMode {
return template.ParseFiles("templates/index.html")
} else {
data, err := Asset("templates/index.html")
if err != nil {
return nil, err
}
return template.New("templates/index.html").Parse(string(data))
}
}
func ListenAndServe(port int, hub *Hub, layout *Layout, fsMode bool) error {
t, err := LoadTemplate(fsMode)
if err != nil {
return err
}
addr := fmt.Sprintf(":%d", port)
fmt.Printf("Starting HTTP server on localhost%s\n", addr)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := t.Execute(w, map[string]interface{}{"Port": port, "Layout": *layout})
if err != nil {
fmt.Println("Error rendering response:", err)
}
})
if fsMode {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
} else {
http.Handle("/static/", http.FileServer(&assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
AssetInfo: AssetInfo,
Prefix: "",
}))
}
http.HandleFunc("/updates", func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Could not upgrade:", err)
return
}
client := &Client{
hub: hub,
conn: conn,
dataCh: make(chan []byte, 10),
}
defer func() {
client.hub.leaveCh <- client
client.conn.Close()
}()
hub.enterCh <- client
for {
err := client.conn.WriteMessage(websocket.TextMessage, <-client.dataCh)
if err != nil {
fmt.Println("Could not send:", err)
break
}
}
})
return http.ListenAndServe(addr, nil)
}
func Usage() {
progname := os.Args[0]
fmt.Fprintf(os.Stderr, "Usage of %s:\n", progname)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
Examples:
%s -d=dashboard.json
%s -d=dashboard.json -i=10s
For more details and docs, see README: http://github.com/propan/expvardash
`, progname, progname)
}