This repository has been archived by the owner on Mar 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
dashing.go
156 lines (137 loc) · 4.21 KB
/
dashing.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
package dashing
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"path/filepath"
)
var config *Config
var eventBroker *EventBroker
func renderStatus(w http.ResponseWriter, status int, key string, info string) {
w.WriteHeader(status)
json.NewEncoder(w).Encode(map[string]string{key: info})
}
func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if r.Method == "GET" {
dashboards, _ := filepath.Glob(filepath.Join(config.DashingPath, "dashboards", "*.json"))
if len(dashboards) > 0 {
defaultDashboard := config.DefaultDashboard
if len(defaultDashboard) == 0 {
firstDashboard := dashboards[0]
firstDashboard = filepath.Base(firstDashboard)
defaultDashboard = firstDashboard[0 : len(firstDashboard)-5]
}
http.Redirect(w, r, "/"+defaultDashboard, http.StatusMovedPermanently)
} else {
renderStatus(w, 404, "status", "not found")
}
}
}
func dashboard(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
layoutHTMLBytes := MustAsset("layout/dashboard.html")
w.Write(layoutHTMLBytes)
}
func dashboardPost(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
dashboard := params.ByName("dashboard")
var data map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
renderStatus(w, 400, "status", err.Error())
return
}
token := data["token"]
if token != config.GlobalToken {
renderStatus(w, 403, "status", "forbidden")
return
}
delete(data, "token")
eventBroker.events <- &Event{dashboard, "dashboard", dashboard, data}
renderStatus(w, 200, "status", "ok")
}
func widgetPost(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
dashboard := params.ByName("dashboard")
widget := params.ByName("widget")
var data map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
renderStatus(w, 400, "status", err.Error())
return
}
token := data["token"]
if token != config.GlobalToken &&
token != config.HackToken {
widgetToken := config.WidgetTokens[widget]
if len(widgetToken) == 0 || token != widgetToken {
renderStatus(w, 403, "status", "forbidden")
return
}
}
delete(data, "token")
eventBroker.events <- &Event{dashboard, "widget", widget, data}
renderStatus(w, 200, "status", "ok")
}
func events(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
dashboard := params.ByName("dashboard")
f, ok := w.(http.Flusher)
if !ok {
renderStatus(w, 500, "status", "error: streaming unsupported")
return
}
closeNotification, ok := w.(http.CloseNotifier)
if !ok {
renderStatus(w, 500, "status", "error: close notification unsupported")
return
}
events := make(chan *Event)
eventBroker.newClients <- events
defer func() {
eventBroker.defunctClients <- events
}()
w.Header().Set("Content-Type", "text/event-stream;charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("X-Accel-Buffering", "no")
closer := closeNotification.CloseNotify()
for {
select {
case event := <-events:
if event.Dashboard == dashboard || event.Dashboard == "*" {
data := event.Body
data["id"] = event.ID
if event.Target != "" {
w.Write([]byte("event: "))
w.Write([]byte(event.Target))
w.Write([]byte("\n"))
}
raw, _ := json.Marshal(data)
w.Write([]byte("data: "))
w.Write(raw)
w.Write([]byte("\n\n"))
f.Flush()
}
case <-closer:
break
}
}
}
func Server(dashingPath string, host string, port int) error {
config = NewConfig(filepath.Join(dashingPath, "config.json"))
config.DashingPath = dashingPath
eventBroker = NewEventBlock()
eventBroker.Start()
for _, j := range registry {
go j.Work(eventBroker.events)
}
router := httprouter.New()
router.GET("/", index)
router.GET("/:dashboard", dashboard)
router.POST("/dashboards/:dashboard", dashboardPost)
router.POST("/dashboards/:dashboard/widgets/:widget", widgetPost)
router.GET("/:dashboard/events", events)
router.NotFound = http.FileServer(http.Dir(filepath.Join(dashingPath, "public")))
addr := fmt.Sprintf("%s:%d", host, port)
fmt.Printf("Listening on %s ...\n", addr)
if err := http.ListenAndServe(addr, router); err != nil {
return err
}
return nil
}