/
goclubby.go
368 lines (316 loc) · 10.9 KB
/
goclubby.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"time"
"io"
"io/ioutil"
"bytes"
"net/http"
"os"
"os/exec"
"crypto/md5"
"hash"
"log"
"fmt"
"strings"
"encoding/json"
"encoding/hex"
"runtime"
"flag"
//"reflect"
"github.com/howeyc/fsnotify"
)
// interface for mapping.json
var Mapper interface {}
// interface for virtual_hosts.json
var VirtualHoster interface {}
type Resource struct {
resourceData []byte
order int
}
type HostConfig struct {
root, mode, mapping string
}
type ResourceProperty struct {
resourcePath string
minifyLevel int
}
type ResourceMapping struct {
clubbedResourcePath string
resources []ResourceProperty
}
type HostMapping struct {
clubbedResources []ResourceMapping
}
// key - host
// value - HostMapping struct
var hostMappings map[string]HostMapping = make(map[string]HostMapping)
// key - host
// value - HostConfig struct
var hostConfigs map[string]HostConfig = make(map[string]HostConfig)
var compilationLevel = map[int]string{1: "WHITESPACE_ONLY",
2: "SIMPLE_OPTIMIZATIONS",
3: "ADVANCED_OPTIMIZATIONS"}
var numCores = flag.Int("n", runtime.NumCPU(), "number of CPU cores to use")
var basePath, _ = os.Getwd()
var prodDir = "./tmp/"
var testPageHTML = `<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>goclubby test page</title>
<script type="text/javascript" src="/js/file1.js"></script>
<script type="text/javascript" src="/js/file2.js"></script>
<script type="text/javascript" src="/js/file3.js"></script>
</head>
<body>
<b>
Open up console and test
</b>
</body>
</html>
`
func testPage(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Server", "goclubby/0.1")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Content-Type", "text/html; charset=iso-8859-1")
io.WriteString(w, testPageHTML)
}
func readResource(out chan *Resource, resourcePath string, minify int, order int) {
var msg *Resource = new(Resource)
if minify == 0 {
// read the byte data normally
resourceData, err := ioutil.ReadFile(resourcePath)
if err != nil {
fmt.Printf("###\nError - %s\n%s\n###\n", resourcePath, err)
os.Exit(1)
}
(*msg).resourceData = resourceData
} else {
// get the minified byte data using closure compiler
resourceData, err := exec.Command("java", "-jar",
"closure/compiler.jar",
"--compilation_level",
compilationLevel[minify],
"--js", resourcePath ).Output()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
(*msg).resourceData = resourceData
}
(*msg).order = order
out <- msg
}
func concatResource(in chan *Resource, numResources int) (clubbedResource [][]byte) {
count := 0
clubbedResource = make([][]byte, numResources)
for resource := range in {
count = count + 1
clubbedResource[resource.order] = resource.resourceData
if count == numResources {
// close the channel so that for-loop stops
// listening for incoming items and breaks the loop
close(in)
}
}
return
}
func getClubbedResource(resources []ResourceProperty) (clubbed_resource []byte) {
var recv chan *Resource
// same channel is used for communication between
// readResource goroutine and writeResource function
// and it is a buffered channel for asynchronous messaging
recv = make(chan *Resource, len(resources))
for order, resourceProperty := range resources {
// create a goroutine on every I/O operation so that
// multiple I/O operations happen in parallel
go readResource(recv, basePath + resourceProperty.resourcePath,
resourceProperty.minifyLevel, order)
}
clubbed_resource = bytes.Join(concatResource(recv, len(resources)),
[]byte{})
return
}
func serverInit(w http.ResponseWriter, req *http.Request) {
var readPath string
var md5Hash hash.Hash
var resourceData []byte
var err error
w.Header().Set("Server", "goclubby/0.1")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Content-Type", "application/javascript")
response := ""
hostPort := strings.Split(req.Host, ":")
host := hostPort[0]
if(!strings.Contains(host, ".")) {
hostConfig := hostConfigs[host]
w.Header().Set("X-mode", hostConfig.mode)
if(hostConfig.mode == "dev"){
// Mode - dev
hostMapping := hostMappings[host]
for _, resourceMapping := range hostMapping.clubbedResources {
if(resourceMapping.clubbedResourcePath == req.URL.Path) {
response = string(getClubbedResource(resourceMapping.resources))
} else {
fmt.Printf("###\nError - %s does'nt exist in %s's mapping.json!\n###\n",
req.URL.Path, host)
}
}
} else {
// Mode - production
md5Hash = md5.New()
md5Hash.Write([]byte(req.URL.Path))
readPath = prodDir + host + "/" +
hex.EncodeToString(md5Hash.Sum(nil))
resourceData, err = ioutil.ReadFile(readPath)
if err != nil {
fmt.Printf("###\nError - %s\n%s\n###\n", req.URL.Path, err)
}
response = string(resourceData)
}
}
io.WriteString(w, response)
if err == nil {
fmt.Printf("Response sent- %s %s\n", req.URL, time.Now())
}
}
func readResourceMapping(mappingPath string) {
// read configuration from mapping.json
mappingData, err := ioutil.ReadFile(mappingPath)
if err != nil {
fmt.Printf("Error occured in %s\n", err)
os.Exit(1)
}
// decode json to Mapping data structure in go
err = json.Unmarshal(mappingData, &Mapper)
if err != nil {
fmt.Printf("Error occured in %s\n", err)
os.Exit(1)
}
}
func readHostsConfig() {
// read configuration from virtual_hosts.json
hostsData, err := ioutil.ReadFile("virtual_hosts.json")
if err != nil {
fmt.Printf("Error occured in %s\n", err)
os.Exit(1)
}
// decode json to VirtualHost data structure in go
err = json.Unmarshal(hostsData, &VirtualHoster)
if err != nil {
fmt.Printf("Error occured in %s\n", err)
os.Exit(1)
}
configMap := VirtualHoster.(map[string]interface{})
hostConfig := new(HostConfig)
for domain, setting := range configMap {
for k, v := range setting.(map[string]interface{}) {
switch field := k; field {
case "root" : (*hostConfig).root = v.(string)
case "mode" : (*hostConfig).mode = v.(string)
case "mapping" : (*hostConfig).mapping = v.(string)
default :
fmt.Printf("Unknown config key, " +
"Please check the syntax and spellings: %s\n", field)
os.Exit(1)
}
}
hostConfigs[domain] = *hostConfig
}
//fmt.Printf("host config: %#v\n\n", hostConfigs)
}
func hostMappingSetup() {
hostMapping := new(HostMapping)
resourceMapping := new(ResourceMapping)
resource_property := new(ResourceProperty)
var count int
for host, config := range hostConfigs {
readResourceMapping(config.mapping)
var resourceMappingSlice []ResourceMapping =
make([]ResourceMapping, len(Mapper.(map[string]interface{})))
count = 0
for clubbedResource, resourceList := range Mapper.(map[string]interface{}) {
(*resourceMapping).clubbedResourcePath = clubbedResource
resource_List := resourceList.([]interface{})
var resourcePropertySlice []ResourceProperty =
make([]ResourceProperty, len(resource_List))
for order, resourceProperty := range resource_List {
for resourcePath, minify := range resourceProperty.(map[string]interface{}) {
(*resource_property).resourcePath = resourcePath
(*resource_property).minifyLevel = int(minify.(float64))
}
resourcePropertySlice[order] = *resource_property
}
(*resourceMapping).resources = resourcePropertySlice
resourceMappingSlice[count] = *resourceMapping
count = count + 1
}
(*hostMapping).clubbedResources = resourceMappingSlice
hostMappings[host] = *hostMapping
}
}
func hostModeSetup() {
var mapping HostMapping
var clubbedResource []byte
var err error
var writePath string
var md5Hash hash.Hash
for host, config := range hostConfigs {
if(config.mode == "production") {
err = os.Mkdir(prodDir + host, 0777)
if err != nil {
fmt.Printf("###\nWarning - %s\n###\n", err)
}
mapping = hostMappings[host]
for _, resourceMapping := range mapping.clubbedResources {
clubbedResource = getClubbedResource(resourceMapping.resources)
md5Hash = md5.New()
md5Hash.Write([]byte(resourceMapping.clubbedResourcePath))
writePath = prodDir + host + "/" + hex.EncodeToString(md5Hash.Sum(nil))
fmt.Printf("Production Mode for %s - write file: %s\n",
host, writePath)
ioutil.WriteFile(writePath, clubbedResource, 0666)
}
}
}
}
func ExampleNewWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
go func() {
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
err = watcher.Watch("./tmp/localhost")
err = watcher.Watch("./tmp/a")
err = watcher.Watch("./tmp/b")
if err != nil {
log.Fatal(err)
}
fmt.Printf("watching ./tmp , ./tmp/a and ./tmp/b ")
}
func main() {
// make sure app uses all cores
flag.Parse()
runtime.GOMAXPROCS(*numCores)
readHostsConfig()
hostMappingSetup()
hostModeSetup()
ExampleNewWatcher()
fmt.Printf("\ngoclubby server running at " +
"http://0.0.0.0:8000 on %d CPU cores\n", *numCores)
http.HandleFunc("/", testPage)
http.HandleFunc("/js/", serverInit)
err := http.ListenAndServe("0.0.0.0:8000", nil)
if err != nil {
log.Fatal("In main(): ", err)
}
}