-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
93 lines (80 loc) · 2.55 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
var express = require('express'),
http = require('http'),
path = require('path'),
hbs = require('express-hbs'),
storage = require('node-persist'),
scheduler = require('node-schedule'),
deferred = require('deferred'),
xhr = require('xmlhttprequest').XMLHttpRequest,
meddle = require('./public/javascripts/meddle.js');
var tasks = {
sites: {
job: require('./tasks/sites.js'),
schedule: { dayOfWeek: 0, hour: 3, minute: 0 }
},
badges: {
job: require('./tasks/badges.js'),
schedule: { dayOfWeek: 0, hour: 3, minute: 5 }
},
elections: {
job: require('./tasks/elections.js'),
schedule: { minute: new scheduler.Range(5, 55, 10) }
}
};
// Background task setup
storage.initSync({ dir: '../../../data' });
meddle.Configure({
requestHandler: xhr,
key: 'LYwuXpduJM2Kk9Twsc008w((',
debug: true
});
var promises = [];
Object.keys(tasks).forEach(function (key) {
promises.push(deferred.promisify(tasks[key].job.init)({
storage: storage,
meddle: meddle,
xhr: xhr
}));
});
// Defer starting Express until we have the prerequisite template data
deferred.apply(null, promises)
.then(scheduleTasks)
.then(startExpress)
.done();
function scheduleTasks() {
Object.keys(tasks).forEach(function (key) {
scheduler.scheduleJob(tasks[key].schedule, tasks[key].job.update);
});
}
// Express setup
function startExpress() {
var app = express();
app.engine('hbs', hbs.express3({
partialsDir: __dirname + '/views/partials',
contentHelperName: 'content'
}));
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'hbs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
var routes = require('./routes')({
storage: storage,
meddle: meddle,
xhr: xhr
});
app.get('/', routes.index);
app.get('/:site/election', routes.election);
app.get('/:site/candidate-tags/:id', routes.candidateTags);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
}