forked from tobz/cadastre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
306 lines (240 loc) · 8.81 KB
/
web.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package cadastre
import "fmt"
import "log"
import "os"
import "io"
import "strings"
import "sync/atomic"
import "time"
import "bytes"
import "path/filepath"
import "html/template"
import "encoding/base64"
import "encoding/json"
import "compress/gzip"
import "net/http"
import "github.com/howeyc/fsnotify"
import "github.com/tobz/gocache"
import "github.com/gorilla/mux"
type WebUI struct {
Configuration *Configuration
server *http.Server
templateCache *gocache.Cache
templateWatcher *fsnotify.Watcher
RequestsServed uint64
RequestErrors uint64
CacheRequests uint64
CacheHits uint64
}
func (me *WebUI) StartListening() error {
var err error
// Initiailize our statistics.
me.RequestsServed = 0
me.RequestErrors = 0
me.CacheRequests = 0
me.CacheHits = 0
log.Printf("Instantiating template cache...")
// Set up our template cache and template watcher.
me.templateCache = gocache.New((time.Minute * 30), (time.Second * 30))
me.templateWatcher, err = fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("Unable to instantiate template watcher! %s", err)
}
go func() {
for {
select {
// Something happened in our template directory. Invalidate the cache.
case ev := <-me.templateWatcher.Event:
log.Printf("Detected change to template file '%s'. Flushing template cache.", ev.Name)
me.templateCache.Clear()
}
}
}()
absTemplateDirectory, err := filepath.Abs(me.Configuration.TemplateDirectory)
if err != nil {
return fmt.Errorf("Caught an error trying to get the absolute path to the template directory! %s", err)
}
// Watch our template directory for changes so that we invalidate the cache Only watch for modifications and deletes.
me.templateWatcher.WatchFlags(absTemplateDirectory, fsnotify.FSN_MODIFY)
log.Printf("Creating HTTP server...")
// Set up our request multiplexer.
requestMultiplexer := mux.NewRouter()
// Create our server instance using our request multiplexer.
me.server = &http.Server{
Addr: me.Configuration.ListenAddress,
Handler: requestMultiplexer,
}
log.Printf("Configuring request router...")
// Define our routes.
requestMultiplexer.Handle("/favicon.ico", CadastreHandler(me, me.serveFavicon))
requestMultiplexer.Handle("/", CadastreHandler(me, me.serveIndex))
requestMultiplexer.Handle("/_getServerGroups", CadastreHandler(me, me.serveServerGroups))
requestMultiplexer.Handle("/_getCurrentSnapshot/{serverName}", CadastreHandler(me, me.serveCurrentSnapshot))
absStaticAssetPath, err := filepath.Abs(me.Configuration.StaticAssetDirectory)
if err != nil {
return fmt.Errorf("Caught an error trying to get the absolute path to the static asset directory! %s", err)
}
requestMultiplexer.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(absStaticAssetPath))))
// Start listening!
go func() {
err := me.server.ListenAndServe()
if err != nil {
log.Printf("Caught an error while serving requests! %s", err)
}
}()
return nil
}
func (me *WebUI) loadTemplate(templatePath string) (*template.Template, error) {
incrementCounter(&(me.CacheRequests))
// Try the cache first.
cachedTemplate, found := me.templateCache.Get(templatePath)
if found {
incrementCounter(&(me.CacheHits))
return cachedTemplate.(*template.Template), nil
}
// Not in the cache, so we need to go to disk for it. Build our path.
absTemplatePath, err := filepath.Abs(filepath.Join(me.Configuration.TemplateDirectory, templatePath))
if err != nil {
return nil, fmt.Errorf("Error building the proper path to load the template file! %s", err)
}
// Open the template.
fileHandle, err := os.Open(absTemplatePath)
if err != nil {
return nil, fmt.Errorf("Error loading template file! %s", err)
}
defer fileHandle.Close()
// Read out the template.
templateBuffer := bytes.NewBuffer([]byte{})
_, err = templateBuffer.ReadFrom(fileHandle)
if err != nil {
return nil, fmt.Errorf("Error reading the template file! %s", err)
}
// Create our actual template.
newTemplate := template.New(templatePath)
newTemplate, err = newTemplate.Parse(templateBuffer.String())
if err != nil {
return nil, fmt.Errorf("Error creating template object! %s", err)
}
// Put this in the cache.
me.templateCache.Set(templatePath, newTemplate, -1)
return newTemplate, nil
}
func (me *WebUI) serveFavicon(response http.ResponseWriter, request *http.Request) error {
response.Header().Add("Content-Type", "image/x-icon")
imageData, err := base64.StdEncoding.DecodeString(CadastreEncodedFavicon)
if err != nil {
serveErrorPage(response)
return fmt.Errorf("request error: couldn't decode favicon data")
}
response.Write(imageData)
return nil
}
func (me *WebUI) serveIndex(response http.ResponseWriter, request *http.Request) error {
return me.renderTemplate(response, "index.goml", nil)
}
func (me *WebUI) serveServerGroups(response http.ResponseWriter, request *http.Request) error {
// Wrap our result.
result := make(map[string]interface{}, 0)
result["groups"] = me.Configuration.ServerGroups
return me.renderJson(response, result)
}
func incrementCounter(addr *uint64) {
atomic.AddUint64(addr, 1)
}
func (me *WebUI) serveCurrentSnapshot(response http.ResponseWriter, request *http.Request) error {
// Get the specified server name.
requestVars := mux.Vars(request)
internalName := requestVars["serverName"]
// Try and find the given server in our list of servers.
for _, server := range me.Configuration.Servers {
if server.InternalName == internalName {
// Found our server, so try and take a snapshot.
newSnapshot := &Snapshot{}
err := newSnapshot.TakeSnapshot(server)
if err != nil {
return me.renderJsonError(response, fmt.Sprintf("Failed to take processlist snapshot for <b>%s</b>! %s", internalName, err.Error()))
}
return me.renderJson(response, newSnapshot)
}
}
// We never found the server, so inform the UI. This is definitely a bug.
return me.renderJsonError(response, fmt.Sprintf("Failed to find server <b>%s</b> in the configured list of servers!", internalName))
}
func (me *WebUI) renderJson(response http.ResponseWriter, data interface{}) error {
response.Header().Set("Content-Type", "application/json")
result := make(map[string]interface{}, 0)
result["success"] = true
result["payload"] = data
jsonEncoder := json.NewEncoder(response)
err := jsonEncoder.Encode(result)
if err != nil {
serveErrorPage(response)
return fmt.Errorf("request error: failed to encode object to JSON! %s", err)
}
return nil
}
func (me *WebUI) renderJsonError(response http.ResponseWriter, errorMessage string) error {
response.Header().Set("Content-Type", "application/json")
result := make(map[string]interface{}, 0)
result["success"] = false
result["errorMessage"] = errorMessage
jsonEncoder := json.NewEncoder(response)
err := jsonEncoder.Encode(result)
if err != nil {
serveErrorPage(response)
return fmt.Errorf("request error: failed to encode error object to JSON! %s", err)
}
return nil
}
func (me *WebUI) renderTemplate(response http.ResponseWriter, templatePath string, content map[string]interface{}) error {
response.Header().Set("Content-Type", "text/html")
// Load the specified template.
templateData, err := me.loadTemplate(templatePath)
if err != nil {
serveErrorPage(response)
return fmt.Errorf("request error: couldn't load template '%s': %s", templatePath, err)
}
// Generate our template output and write it to the response.
err = templateData.Execute(response, content)
if err != nil {
serveErrorPage(response)
return fmt.Errorf("request error: couldn't render template '%s': %s", templatePath, err)
}
return nil
}
func serveErrorPage(response http.ResponseWriter) {
response.Header().Set("Content-Type", "text/plain")
errorOutput := "Sorry, something went terribly wrong here. :("
response.Write([]byte(errorOutput))
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (me *gzipResponseWriter) Write(b []byte) (int, error) {
return me.Writer.Write(b)
}
type CadastreWebHandler struct {
Handler func(http.ResponseWriter, *http.Request) error
Server *WebUI
}
func (me *CadastreWebHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
var err error
// If the browser accepts gzip'd content, use that.
if strings.Contains(request.Header.Get("Accept-Encoding"), "gzip") {
response.Header().Set("Content-Encoding", "gzip")
gzipWriter := gzip.NewWriter(response)
defer gzipWriter.Close()
gzipResponse := &gzipResponseWriter{Writer: gzipWriter, ResponseWriter: response}
err = me.Handler(gzipResponse, request)
} else {
err = me.Handler(response, request)
}
if err != nil {
incrementCounter(&(me.Server.RequestErrors))
}
incrementCounter(&(me.Server.RequestsServed))
}
func CadastreHandler(server *WebUI, handlerFunc func(http.ResponseWriter, *http.Request) error) *CadastreWebHandler {
return &CadastreWebHandler{Handler: handlerFunc, Server: server}
}