-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (81 loc) · 3.18 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var debug = require('debug')('sync');
var app = require('./app');
var db = require('./db');
var App = function() {
// Scope.
var self = this;
/* ================================================================ */
/* Helper functions. */
/* ================================================================ */
self.setupVariables = function() {
// Set the environment variables we need.
self.port = process.env.PORT || 5000;
self.dbConnectionURL = process.env.DATABASE_URL; // != null ? process.env.MONGODB_URI + 'learning' : 'mongodb://localhost:27017/users';
};
/**
* terminator === the termination handler
* Terminate server on receipt of the specified signal.
* @param {string} sig Signal to terminate on.
*/
self.terminator = function(sig){
if (typeof sig === "string") {
console.log('%s: Received %s - terminating sample app ...',
Date(Date.now()), sig);
process.exit(1);
}
console.log('%s: Node server stopped.', Date(Date.now()) );
};
/**
* Setup termination handlers (for exit and a list of signals).
*/
self.setupTerminationHandlers = function(){
// Process on exit and signals.
process.on('exit', function() { self.terminator(); });
// Removed 'SIGPIPE' from the list - bugz 852598.
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(function(element, index, array) {
process.on(element, function() { self.terminator(element); });
});
};
/* ================================================================ */
/* App server functions (main app logic here). */
/* ================================================================ */
/**
* Initializes the sample application.
*/
self.initialize = function() {
self.setupVariables();
self.setupTerminationHandlers();
};
/**
* Start the server (starts up the sample application).
*/
self.start = function() {
if (app.get('env') === 'development') {
app.listen(self.port, self.ipaddress, function() {
debug('%s: Node server started on %s:%d ...',
Date(Date.now() ), self.ipaddress, self.port);
});
} else {
db.connect(self.dbConnectionURL, function(err) {
if (err) {
console.log('Unable to connect to Postgres. '+err);
// process.exit(1); // TODO: check why
} else {
// Start the app on the specific interface (and port).
app.listen(self.port, self.ipaddress, function() {
debug('%s: Node server started on %s:%d ...',
Date(Date.now() ), self.ipaddress, self.port);
});
}
});
}
};
};
/**
* main(): Main code.
*/
var mainapp = new App();
mainapp.initialize();
mainapp.start();