-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
121 lines (102 loc) · 2.76 KB
/
app.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
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
/*
* Main server router
*/
var http = require('http'),
express = require('express'),
morgan = require('morgan'),
moment = require('moment'),
compression = require('compression'),
cors = require('cors'),
config = require('./config.js'),
process = require('./process.js');
(function checkConfigSanity()
{
console.log('Verifying config.json');
// verify sane collection dependencies
var cs = Object.keys(config.collections);
var refreshable = true;
var i = 0;
while( i < cs.length )
{
var c = config.collections[cs[i]];
if( c.initialQuery && c.sharesDataWith || !c.initialQuery && !c.sharesDataWith )
{
console.log('Collection "'+cs[i]+'" does not have one data source, ignoring.');
delete config.collections[cs[i]];
cs.splice(i,1);
}
else if( c.sharesDataWith && !config.collections[c.sharesDataWith] )
{
console.log('Dependency of "'+cs[i]+'" does not exist, ignoring.');
delete config.collections[cs[i]];
cs.splice(i,1);
}
else if( c.sharesDataWith && !config.collections[c.sharesDataWith].initialQuery )
{
console.log('Dependency of "'+cs[i]+'" is not independent, ignoring.');
delete config.collections[cs[i]];
cs.splice(i,1);
}
else if( c.sharesDataWith && i < cs.indexOf(c.sharesDataWith) ){
cs.push(cs.splice(i,1));
}
else {
refreshable |= c.manualRefresh;
i++;
}
}
config.refreshOrder = cs;
config.globalRefresh = refreshable;
})();
process.intervalRefresh(function()
{
var app = express();
app.use(compression());
app.use(morgan('combined'));
app.use(cors());
app.post('/:collectId/refresh', function(req,res,next)
{
if( config.collections[req.params.collectId] )
{
if(config.collections[req.params.collectId].manualRefresh === true)
process.refresh(req.params.collectId, req,res);
else
res.status(403).send();
}
else {
next();
}
});
app.get('/:collectId', function(req,res,next)
{
if( config.collections[req.params.collectId] )
process.serveResults(req.params.collectId, req,res);
else
next();
});
app.post('/refresh', function(req,res,next)
{
if(config.globalRefresh){
process.intervalRefresh(function(){
res.status(200).send();
});
}
else
next();
});
app.get('/', function(req,res,next){
res.send(Object.keys(config.collections));
});
// all else fails, 404
app.use(function(req,res){
res.status(404).send('<h1>404 Not Found</h1>');
});
if(config.refreshInterval){
var timeparts = config.refreshInterval.split(' ');
var interval = moment.duration(parseInt(timeparts[0]), timeparts[1]);
setInterval(process.intervalRefresh, interval.asMilliseconds());
console.log('Setting automatic cache refresh in '+interval.humanize());
}
http.createServer(app).listen(config.port);
console.log('Listening on port '+config.port);
});