-
Notifications
You must be signed in to change notification settings - Fork 20
/
server.go
193 lines (159 loc) · 4.59 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
package dashing
import (
"encoding/json"
"fmt"
"log"
"net/http"
"path/filepath"
"time"
"gopkg.in/husobee/vestigo.v1"
"gopkg.in/karlseguin/gerb.v0"
)
// A Server contains webservice parameters and middlewares.
type Server struct {
dev bool
webroot string
broker *Broker
}
func param(r *http.Request, name string) string {
return r.FormValue(fmt.Sprintf(":%s", name))
}
// IndexHandler redirects to the default dashboard.
func (s *Server) IndexHandler(w http.ResponseWriter, r *http.Request) {
files, _ := filepath.Glob("dashboards/*.gerb")
for _, file := range files {
dashboard := file[11 : len(file)-5]
if dashboard != "layout" {
http.Redirect(w, r, fmt.Sprintf("/%s", dashboard), http.StatusTemporaryRedirect)
return
}
}
http.NotFound(w, r)
}
// EventsHandler opens a keepalive connection and pushes events to the client.
func (s *Server) EventsHandler(w http.ResponseWriter, r *http.Request) {
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}
c, ok := w.(http.CloseNotifier)
if !ok {
http.Error(w, "Close notification unsupported!", http.StatusInternalServerError)
return
}
// Create a new channel, over which the broker can
// send this client events.
events := make(chan *Event)
// Add this client to the map of those that should
// receive updates
s.broker.newClients <- events
// Remove this client from the map of attached clients
// when the handler exits.
defer func() {
s.broker.defunctClients <- events
}()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("X-Accel-Buffering", "no")
closer := c.CloseNotify()
for {
select {
case event := <-events:
data := event.Body
data["id"] = event.ID
data["updatedAt"] = int32(time.Now().Unix())
json, err := json.Marshal(data)
if err != nil {
continue
}
if event.Target != "" {
fmt.Fprintf(w, "event: %s\n", event.Target)
}
fmt.Fprintf(w, "data: %s\n\n", json)
f.Flush()
case <-closer:
log.Println("Closing connection")
return
}
}
}
// DashboardHandler serves the dashboard layout template.
func (s *Server) DashboardHandler(w http.ResponseWriter, r *http.Request) {
dashboard := param(r, "dashboard")
if dashboard == "" {
dashboard = fmt.Sprintf("events%s", param(r, "suffix"))
}
template, err := gerb.ParseFile(true, fmt.Sprintf("dashboards/%s.gerb", dashboard), "dashboards/layout.gerb")
if err != nil {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
template.Render(w, map[string]interface{}{
"dashboard": dashboard,
"development": s.dev,
"request": r,
})
}
// DashboardEventHandler accepts dashboard events.
func (s *Server) DashboardEventHandler(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
defer r.Body.Close()
}
var data map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
s.broker.events <- &Event{param(r, "id"), data, "dashboards"}
w.WriteHeader(http.StatusNoContent)
}
// WidgetHandler serves widget templates.
func (s *Server) WidgetHandler(w http.ResponseWriter, r *http.Request) {
widget := param(r, "widget")
widget = widget[0 : len(widget)-5]
template, err := gerb.ParseFile(true, fmt.Sprintf("widgets/%s/%s.html", widget, widget))
if err != nil {
log.Printf("%v", err)
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
template.Render(w, nil)
}
// WidgetEventHandler accepts widget data.
func (s *Server) WidgetEventHandler(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
defer r.Body.Close()
}
var data map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
log.Printf("%v", err)
http.Error(w, "", http.StatusBadRequest)
return
}
s.broker.events <- &Event{param(r, "id"), data, ""}
w.WriteHeader(http.StatusNoContent)
}
// NewRouter creates a router with defaults.
func (s *Server) NewRouter() http.Handler {
r := vestigo.NewRouter()
r.Get("/", s.IndexHandler)
r.Get("/events", s.EventsHandler)
r.Get("/events:suffix", s.DashboardHandler) // workaround for router edge case
r.Get("/:dashboard", s.DashboardHandler)
r.Post("/dashboards/:id", s.DashboardEventHandler)
r.Get("/views/:widget", s.WidgetHandler)
r.Post("/widgets/:id", s.WidgetEventHandler)
return r
}
// NewServer creates a Server instance.
func NewServer(b *Broker) *Server {
return &Server{
dev: false,
webroot: "",
broker: b,
}
}