2015-06-18 22:01:09 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Steppy = require('twostep').Steppy,
|
|
|
|
_ = require('underscore'),
|
|
|
|
http = require('http');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pure rest api on pure nodejs follows below
|
|
|
|
*/
|
|
|
|
exports.register = function(app) {
|
2015-07-06 17:19:13 +00:00
|
|
|
var config = _(app.config.httpApi).defaults({host: '127.0.0.1', port: 3030}),
|
2015-06-18 22:01:09 +00:00
|
|
|
projects = app.projects,
|
2015-07-05 18:03:58 +00:00
|
|
|
distributor = app.distributor,
|
|
|
|
logger = app.lib.logger('http api');
|
2015-06-18 22:01:09 +00:00
|
|
|
|
|
|
|
var server = http.createServer(function(req, res) {
|
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
var stepCallback = this.slot();
|
|
|
|
|
|
|
|
req.setEncoding('utf-8');
|
|
|
|
var bodyString = '';
|
|
|
|
req.on('data', function(data) {
|
|
|
|
bodyString += data;
|
|
|
|
});
|
|
|
|
req.on('end', function() {
|
|
|
|
var body = JSON.parse(bodyString);
|
|
|
|
stepCallback(null, body);
|
|
|
|
});
|
|
|
|
req.on('error', stepCallback);
|
|
|
|
},
|
|
|
|
function(err, body) {
|
|
|
|
res.statusCode = 404;
|
|
|
|
|
|
|
|
// run building of a project
|
|
|
|
if (req.url === '/builds' && req.method === 'POST') {
|
|
|
|
var projectName = body.project,
|
|
|
|
project = _(projects).findWhere({name: projectName});
|
|
|
|
|
|
|
|
if (project) {
|
|
|
|
res.statusCode = 204;
|
2015-07-05 18:03:58 +00:00
|
|
|
logger.log('Run "' + projectName + '"');
|
2015-06-18 22:01:09 +00:00
|
|
|
distributor.run({
|
|
|
|
projectName: projectName,
|
2015-07-11 10:41:01 +00:00
|
|
|
withScmChangesOnly: body.withScmChangesOnly,
|
2015-06-18 22:01:09 +00:00
|
|
|
initiator: {type: 'httpApi'}
|
2015-07-11 10:41:01 +00:00
|
|
|
});
|
2015-06-18 22:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
res.end();
|
|
|
|
},
|
|
|
|
function(err) {
|
2015-07-05 18:03:58 +00:00
|
|
|
logger.error('Error occurred during request: ', err.stack || err);
|
2015-06-18 22:01:09 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-07-06 17:19:13 +00:00
|
|
|
logger.log('Start listenning on %s:%s', config.host, config.port);
|
2015-06-18 22:01:09 +00:00
|
|
|
server.listen(config.port, config.host);
|
|
|
|
};
|