This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
80 lines (69 loc) · 1.89 KB
/
index.js
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
"use strict"
var http = require('http')
var Manifests = require('./manifests.js')
var router = require('router')
var route = router()
var st = require('st')
var ua = require('universal-analytics')
var visitor = ua('UA-48397837-1')
require('newrelic')
route.get('/index.json', function(req, res) {
res.setHeader('Content-Type', 'text/json')
getManifests(function(err, manifests) {
if(err) res.statusCode = 500, res.end(), console.error('error %s\n', err.message, err.stack)
res.write(JSON.stringify(manifests))
res.end()
})
});
function getManifests(callback) {
callback = callback || function() {}
getManifests.queue = getManifests.queue || []
if (getManifests.manifest && !getManifests.forceLoad) {
return process.nextTick(function() {
callback(null, getManifests.manifest)
})
}
getManifests.queue.push(callback)
if (getManifests.isLoading) return
getManifests.isLoading = true
Manifests(function(err, m) {
if (err) return done(err, m)
getManifests.manifest = m
done(err, m)
})
function done(err, m) {
getManifests.queue.forEach(function(cb) {
cb(err, m)
})
getManifests.queue = []
getManifests.isLoading = false
setTimeout(function() {
getManifests.forceLoad = true
getManifests()
getManifests.forceLoad = false
}, 10 * 60 * 1000) // refresh index every 10 minutes
}
}
getManifests()
var mount = st({
path: __dirname + '/public',
url: '/',
passthrough: true,
cache: {
content: {
max: 1024*1024*64,
maxAge: 1000*60*10,
}
}
})
var server = http.createServer(function(req, res) {
visitor.pageview(req.url, function(err){
if(err) console.err(err.message, err.stack)
}).send()
mount(req, res, function(){
route(req, res)
})
})
server.listen(process.env.PORT || 9009, function() {
console.info('server listening on http://localhost:' + server.address().port)
})