This repository has been archived by the owner on May 15, 2019. It is now read-only.
forked from ivarprudnikov/datadump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
108 lines (91 loc) · 3.01 KB
/
server.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
var express = require('express'),
app = express(),
favicon = require('serve-favicon'),
bodyParser = require('body-parser'),
compress = require('compression'),
logger = require('morgan'),
serveStatic = require('serve-static'),
ejs = require('ejs'),
path = require('path'),
config = require('./server/conf/config'),
mappings = require('./server/conf/mappings'),
authentication = require('./server/conf/authentication'),
bootstrap = require('./server/conf/bootstrap'),
datasource = require('./server/conf/datasource')
;
// CONFIGURE APP
////////////////////////////////
app.disable('x-powered-by');
app.set('port', config.port);
app.set('views', __dirname + config.staticFilesDir);
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
app.use(favicon( path.join( __dirname, config.staticFilesDir, 'favicon.ico'), { maxAge: config.expires.year } ));
if(app.get('env') === 'development' || app.get('env') === 'test'){
app.use(logger('dev'));
} else if(app.get('env') === 'production') {
app.use(logger('combined', {
// remove status codes below 400 from log
skip: function (req, res) { return res.statusCode < 400 }
}));
}
// compress response with zlib if required
app.use(compress());
// accept request with json payloads
app.use(bodyParser.json());
// handle static files TODO: switch to nginx in production
app.use( serveStatic( path.join(__dirname, config.staticFilesDir), { maxAge: config.expires.year } ));
// Set up globals for views
////////////////////////////////////////////////////////
app.locals.user = {};
app.locals.errors = [];
app.locals.oauthTwitter = !!config.oauth.twitter.key;
app.locals.oauthGitHub = !!config.oauth.github.key;
app.locals.oauthFacebook = !!config.oauth.facebook.key;
app.locals.oauthGoogle = !!config.oauth.google.key;
app.locals.target = '';
///////////////////
// attach datasource instance
app.db = datasource;
authentication.init(app);
// Setup routes
mappings.init(app);
// Run bootstrap
bootstrap.init(app).done( function(){
// Finally run the app
app.listen(app.get('port'), function () {
'use strict';
console.log('\n');
console.log('+--------------------------');
console.log(' Environment', app.get('env'));
console.log(' PID %d', process.pid);
console.log(' Listening on port', app.get('port'));
console.log('+--------------------------');
});
});
function exitHandler(options, err) {
if (err) {
console.log(err.stack);
}
if (options.cleanup && app.db){
app.db.disconnect(function(){
console.log('datasource disconnected');
});
}
if (options.exit) {
if(app.db){
app.db.disconnect(function(){
console.log('datasource disconnected');
process.exit(0);
});
} else {
process.exit(0);
}
}
}
// do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
// catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
// catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));