-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
280 lines (222 loc) · 6.34 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"encoding/json"
"golang.org/x/net/websocket"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strconv"
"time"
"github.com/mohnish/dashboard/component"
"github.com/mohnish/dashboard/hub"
"github.com/mohnish/dashboard/subs"
)
const (
StorageDir string = "storage"
PluginsDir string = "plugins"
StorageDirPerm = 0777
PluginsDirPerm = 0777
)
var (
count int = 0
dashHub = hub.New()
ticker = time.NewTicker(time.Second * 10)
port = os.Getenv("PORT")
)
func main() {
// Handle landing page
// This function will
// 1. render a template and load react
// 2. respond with real time data to the front-end
go func() {
defer ticker.Stop()
for {
select {
case client := <-dashHub.Register:
dashHub.Add(client)
case client := <-dashHub.Unregister:
dashHub.Remove(client)
case msg := <-dashHub.Broadcast:
dashHub.Publish(msg)
case <-dashHub.Shutdown:
dashHub.Close()
case <-ticker.C:
log.Println("pruned connections", dashHub.Prune())
log.Println("connected clients", len(dashHub.Clients))
log.Println("active go routines", runtime.NumGoroutine())
PrintMemUsage()
}
}
}()
go func() {
// Spin up the plugins
// 1. read all the json files from /plugins dir
// 2. create subscriptions
// 3. start the subscriptions
// 4. listen to the subscription streams
// 5. push data down to clients every time we get the response
files, err := ioutil.ReadDir(PluginsDir)
if err != nil {
if os.IsNotExist(err) {
log.Println("Plugins dir not found. creating one...")
err := os.Mkdir(PluginsDir, PluginsDirPerm)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
var pluginConfigs []subs.PluginConfig
// FIXME: (MT) this needs to be done concurrently in multiple goroutines
for _, file := range files {
var pluginConfig subs.PluginConfig
// TODO: (MT) Store some sort of invalid files list
// if file.Name() == ".DS_Store" {
// continue
// }
log.Println("reading file", file.Name())
body, _ := ioutil.ReadFile(PluginsDir + "/" + file.Name())
err := json.Unmarshal(body, &pluginConfig)
if err != nil {
log.Fatal(err)
} else {
pluginConfigs = append(pluginConfigs, pluginConfig)
}
}
log.Println("found configs", pluginConfigs)
var subscriptions []subs.Subscription
for _, pluginConfig := range pluginConfigs {
interval, err := strconv.Atoi(pluginConfig.Interval)
if err != nil {
log.Fatal(err)
}
log.Println("subscribing to", pluginConfig.Url, interval)
f := subs.Fetch(pluginConfig.Url, time.Duration(interval)*time.Second)
s := subs.Subscribe(f) // starts the sub
subscriptions = append(subscriptions, s)
}
megaSub := subs.Merge(subscriptions)
for update := range megaSub.Updates() {
log.Println("fetched", update)
if err != nil {
log.Fatal(err)
} else {
go func(up interface{}) {
dashHub.Broadcast <- up
}(update)
}
}
}()
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
log.Println("Requested URL:", r.Method+" "+r.URL.Path)
w.Write([]byte("ok"))
})
http.HandleFunc("/static/css/app.css", func(w http.ResponseWriter, r *http.Request) {
log.Println("Requested URL:", r.Method+" "+r.URL.Path)
contents, _ := ioutil.ReadFile("static/css/app.css")
w.Header().Set("Content-Type", "text/css")
w.Write(contents)
})
http.HandleFunc("/static/js/app.js", func(w http.ResponseWriter, r *http.Request) {
log.Println("Requested URL:", r.Method+" "+r.URL.Path)
contents, _ := ioutil.ReadFile("static/js/app.js")
w.Header().Set("Content-Type", "application/javascript")
w.Write(contents)
})
// casting socket func to websocket handler
http.Handle("/ws", websocket.Handler(socket))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("Requested URL:", r.Method+" "+r.URL.Path)
files, err := ioutil.ReadDir(StorageDir)
if err != nil {
if os.IsNotExist(err) {
log.Println("Storage dir not found. creating one...")
err := os.Mkdir(StorageDir, StorageDirPerm)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
var comps []component.Component
// FIXME: (MT) this needs to be done concurrently in multiple goroutines
for _, file := range files {
var comp component.Component
body, err := ioutil.ReadFile(StorageDir + "/" + file.Name())
if err != nil {
log.Fatal("failed to read file", file.Name(), err)
}
err = json.Unmarshal(body, &comp)
log.Println("~~~", comp)
if err != nil {
log.Fatal(err)
} else {
comps = append(comps, comp)
}
}
t, _ := template.ParseFiles("static/index.html")
t.Execute(w, comps)
})
// Handle push notifications
http.HandleFunc("/push", func(w http.ResponseWriter, r *http.Request) {
log.Println("Requested URL:", r.Method+" "+r.URL.Path)
w.Header().Set("Content-Type", "application/json")
if r.Method != "POST" {
http.NotFound(w, r)
return
}
var comp component.Component
// FIXME: (MT) This is a stream. This needs to be switched to use
// dec.More() with a for loop since the incoming stream `r.Body`
// might be bigger and so it'd take more than once decode call
json.NewDecoder(r.Body).Decode(&comp)
log.Println("Request body", comp.App)
comp.Save()
go func() {
dashHub.Broadcast <- comp
}()
json.NewEncoder(w).Encode(comp)
})
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// Helper functions
// TOOD: (MT) create a struct and send these to the front end
// Use these values in the UI
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Printf("Alloc = %v MiB", bToMb(m.Alloc))
log.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
log.Printf("\tSys = %v MiB", bToMb(m.Sys))
log.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func socket(client *websocket.Conn) {
count++
var goRoutineId = count
heartBeat := time.NewTicker(10 * time.Second)
defer heartBeat.Stop()
log.Println("socket connection established")
go func() {
dashHub.Register <- client
}()
// Heartbeat implementation to keep the conn alive
for {
select {
case <-heartBeat.C:
log.Println("tick", goRoutineId)
err := websocket.JSON.Send(client, ".")
if err != nil {
dashHub.Unregister <- client
return
}
}
}
}