forked from allcount/allcountjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allcount-server-startup.js
53 lines (50 loc) · 2.03 KB
/
allcount-server-startup.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
var _ = require('underscore');
var Q = require('q');
module.exports = function (appService, gitRepoUrl, proxyHandler, injection, httpServer, express, app, appSetup, storageDriver) {
return {
startup: function (onReady) {
var self = this;
appService.compile(function (errors) {
if (errors.length > 0) {
if (onReady) {
onReady(errors);
}
throw new Error(errors.join('\n'));
}
appSetup
.map(function (setup) { return function () { return setup.setup() } })
.reduce(Q.when, Q(null))
.catch(function (error) {
if (onReady) {
onReady(error)
}
throw error;
}).then (function () {
self.server = httpServer(function (req, res) {
proxyHandler(req, res, function () {
app(req, res);
});
}, function() {
console.log('Application server for "' + gitRepoUrl + '" listening on port ' + app.get('port'));
if (onReady) {
onReady();
}
});
}).done();
});
},
stop: function () {
var defer = Q.defer();
console.log("Shutting down HTTP server...");
this.server.close(function () {
defer.resolve(null);
});
return defer.promise.then(function () {
console.log("Closing db connection...");
return storageDriver.closeConnection();
}).then(function () {
console.log('Application server for "' + gitRepoUrl + '" on port ' + app.get('port') + ' has stopped');
})
}
}
};