const httpPort = 12001;
var exec = require('child_process').exec;
var http = require('http');
var colors = require("colors");
var httpServer = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain;charset=utf-8'
});
if (req.method.toUpperCase() != 'POST' && req.method.toUpperCase() != 'GET') {
res.end("Request by GET/POST only!");
return;
}
switch (req.url) {
case "/favicon.ico":
// ignore the web icon request
res.end();
break;
default:
debug("Webhook run " + ("async").red + " ---> " + req.url);
res.end("Webhook Success");
exec("cd /home/wwwroot" + req.url + " && git pull", function(err, stdout, stderr) {
if (err) {
console.log(stdout.red);
debug("Webhook error!");
} else {
console.log(stdout.blue);
debug("Webhook success!");
}
});
}
});
httpServer.listen(httpPort);
debug("Webhook start success!");
function debug(msg) {
console.log(getTime().yellow + "\t" + msg);
}
function getTime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
return hours + ":" + minutes + ":" + seconds;
}