-
Notifications
You must be signed in to change notification settings - Fork 20
/
http.go
304 lines (244 loc) · 7.11 KB
/
http.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
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
rice "github.com/GeertJohan/go.rice"
"github.com/desertbit/glue"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
)
var (
distBox *rice.Box
webInterfaceRunning bool
webInterfaceRunningMutex = &sync.Mutex{}
hostName = "localhost"
glueServer *glue.Server
glueServerMutex = &sync.Mutex{}
socketstore *SocketStore
socketstoreMutex = &sync.Mutex{}
// ErrReadingRandomString means reading the random data failed
ErrReadingRandomString = errors.New("failed to read random data")
)
// router for the REST API
func createRouter() *httprouter.Router {
r := httprouter.New()
r.HandlerFunc("GET", "/files/:type/:file", serveFiles)
r.HandlerFunc("GET", "/", serveHTTP)
r.HandlerFunc("GET", "/quit", quitHandler)
r.HandlerFunc("GET", "/wiki", wikiIndexHandler)
r.HandlerFunc("GET", "/wiki/docs/:doc", wikiDocsHandler)
r.HandlerFunc("GET", "/glue/ws", glueWebSocketHandler)
r.HandlerFunc("POST", "/glue/ajax", glueAjaxHandler)
return r
}
// glue handler for web sockets
var glueWebSocketHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
glueServer.ServeHTTP(w, r)
})
// glue handler for ajax requests
var glueAjaxHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
glueServer.ServeHTTP(w, r)
})
// StartWebListener fires up the web interface
func StartWebListener(openInBrowser bool) {
var cLog = Log.WithField("prefix", "StartWebListener")
// check if its already running
webInterfaceRunningMutex.Lock()
if webInterfaceRunning {
webInterfaceRunningMutex.Unlock()
if openInBrowser {
if runtime.GOOS == "darwin" {
open("http://" + hostName + ":" + strconv.Itoa(conf.fields.PortWebPanel))
}
return
}
return
}
webInterfaceRunning = true
webInterfaceRunningMutex.Unlock()
distBox = rice.MustFindBox("frontend/dist")
showNote("serving on "+strconv.Itoa(conf.fields.PortWebPanel), "starting server...")
socketstoreMutex.Lock()
socketstore = NewSocketStore()
socketstoreMutex.Unlock()
go runGlue()
// init router
r := createRouter()
conf.Lock()
if conf.fields.Debug {
// start asset watchers for development
go startJSWatcher()
go startSassWatcher()
}
conf.Unlock()
// listen and serve
err := http.ListenAndServe(":"+strconv.Itoa(conf.fields.PortWebPanel), r)
if err != nil {
cLog.WithError(err).Error("failed to listen")
}
}
// serve index page
var serveHTTP = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "invalid method, only GET allowed", http.StatusMethodNotAllowed)
return
}
c, err := distBox.Bytes("html/index.html")
if err != nil {
Log.WithError(err).Error("failed to serve index page")
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
w.Write(c)
})
// serve assets
var serveFiles = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// get file name
fileName := strings.TrimPrefix(r.RequestURI, "/files/")
if strings.Contains(fileName, "?") {
slice := strings.Split(fileName, "?")
fileName = slice[0]
}
Log.Debug("serveFiles: ", fileName)
// get file contents
b, err := distBox.Bytes(fileName)
if err != nil {
Log.WithError(err).Error("unknown file")
w.WriteHeader(404)
w.Write([]byte("Not found"))
return
}
// set Content-Length
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
// set Content-Type
switch {
case strings.HasPrefix(r.RequestURI, "/files/js"):
w.Header().Set("Content-Type", "text/javascript")
case strings.HasPrefix(r.RequestURI, "/files/css"):
w.Header().Set("Content-Type", "text/css")
case strings.HasSuffix(r.RequestURI, ".mp4"):
w.Header().Set("Content-Type", "video/mp4")
case strings.HasSuffix(r.RequestURI, ".mov"):
w.Header().Set("Content-Type", "video/quicktime")
default:
w.Header().Set("Content-Type", http.DetectContentType(b))
Log.Debug("URI=", r.RequestURI, " CONTENT_TYPE=", http.DetectContentType(b))
}
w.WriteHeader(200)
w.Write(b)
})
// run the glue server
func runGlue() {
glueServerMutex.Lock()
// create a new glue server
glueServer = glue.NewServer(glue.Options{
HTTPListenAddress: ":" + strconv.Itoa(conf.fields.PortGlueServer),
})
// release the glue server on defer
// This will block new incoming connections
// and close all current active sockets
defer glueServer.Release()
// set the glue event function to handle new incoming socket connections
glueServer.OnNewSocket(socketstore.OnNewSocket)
glueServerMutex.Unlock()
// start
err := glueServer.Run()
if err != nil {
Log.WithError(err).Error("glue server failed")
}
}
/*
* Development
*/
// watch JS files and minify on changes
func startJSWatcher() {
var (
id = processID(randomString())
cLog = Log.WithFields(logrus.Fields{
"prefix": "startJSWatcher",
"processID": id,
})
cmd = exec.Command("jsobfus",
"-w",
"frontend/src/js/:frontend/dist/js",
)
)
cLog.Info("starting JS watcher")
err := cmd.Start()
if err != nil {
cLog.WithError(err).Error("JavaScript watcher failed")
return
}
addProcess(id, "jswatcher", cmd.Process, cmd.Process.Pid)
defer deleteProcess(id)
err = cmd.Wait()
if err != nil {
cLog.WithError(err).Error("js watcher failed")
}
}
// start sass watcher and generate css on change
func startSassWatcher() {
var (
id = processID(randomString())
cLog = Log.WithFields(logrus.Fields{
"prefix": "startSassWatcher",
"processID": id,
})
cmd = exec.Command("sasscompile",
"-w",
"frontend/src/sass:frontend/dist/css",
)
)
cLog.Info("starting sass watcher")
err := cmd.Start()
if err != nil {
cLog.WithError(err).Error("sass watcher failed")
return
}
addProcess(id, "sasswatcher", cmd.Process, cmd.Process.Pid)
defer deleteProcess(id)
err = cmd.Wait()
if err != nil {
cLog.WithError(err).Error("sass watcher failed")
}
}
// handle /quit route and exit application
var quitHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "invalid method, only GET allowed", http.StatusMethodNotAllowed)
return
}
Log.Info("exiting, Bye.")
w.WriteHeader(200)
w.Write([]byte("Bye."))
showNote("Bye.", "stopping server and cleaning up")
cleanup(nil)
err := rl.Close()
if err != nil {
Log.WithError(err).Error("failed to close readline")
}
os.Exit(0)
})