-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
61 lines (55 loc) · 1.6 KB
/
main.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
var exec = require('child_process').exec,
Q = require("q"),
http = require("http"),
config = require("./config"),
director = require('director');
var router = new director.http.Router({
'/lollipop3': {
get: function () {
var _this = this;
_this.res.writeHead(200, {"Content-Type": "text/plain"});
doPull()
.then(function (data) {
_this.res.end(data);
}, function (error) {
_this.res.end(error);
});
}
}
});
var server_instance = http.createServer(function(request, response){
router.dispatch(request, response, function (err) {
if (err) {
response.writeHeader(404);
response.write("404");
response.end();
}
});
}).listen(config.http_port);
console.log("Server started.");
/**
*
* @returns {Q.promise}
*/
function doPull() {
var deferred = Q.defer();
exec('git pull', {cwd: config.working_dir},
function callback(error, stdout, stderr) {
if (error) {
deferred.reject(stderr);
console.log(stderr);
} else {
deferred.resolve(stdout);
console.log(stdout);
}
}
);
return deferred.promise;
}
function exitHandler(options, err) {
console.log("Closing.");
server_instance.close();
}
process.on('exit', exitHandler.bind(null,{cleanup:true}));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));