mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-02-11 12:59:15 +00:00
express like wrapper for http api
This commit is contained in:
parent
b2fa9bea11
commit
6697bdd139
69
httpApi.js
69
httpApi.js
@ -7,11 +7,44 @@ var Steppy = require('twostep').Steppy,
|
|||||||
/*
|
/*
|
||||||
* Pure rest api on pure nodejs follows below
|
* Pure rest api on pure nodejs follows below
|
||||||
*/
|
*/
|
||||||
module.exports = function(app) {
|
|
||||||
return function(req, res) {
|
|
||||||
|
|
||||||
var projects = app.projects,
|
var router = {};
|
||||||
distributor = app.distributor;
|
router.routes = {};
|
||||||
|
|
||||||
|
_(['get', 'post', 'patch', 'del']).each(function(method) {
|
||||||
|
router[method] = function(url, handler) {
|
||||||
|
router.routes[method + ' ' + url] = handler;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = function(app) {
|
||||||
|
|
||||||
|
// run building of a project
|
||||||
|
router.post('/api/builds', function(req, res, next) {
|
||||||
|
Steppy(
|
||||||
|
function() {
|
||||||
|
var projectName = req.body.project,
|
||||||
|
project = _(app.projects).findWhere({name: projectName});
|
||||||
|
|
||||||
|
if (project) {
|
||||||
|
res.statusCode = 204;
|
||||||
|
logger.log('Run project "%s"', projectName);
|
||||||
|
app.distributor.run({
|
||||||
|
projectName: projectName,
|
||||||
|
withScmChangesOnly: req.body.withScmChangesOnly,
|
||||||
|
initiator: {type: 'httpApi'}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.statusCode = 404;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.end();
|
||||||
|
},
|
||||||
|
next
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return function(req, res) {
|
||||||
|
|
||||||
Steppy(
|
Steppy(
|
||||||
function() {
|
function() {
|
||||||
@ -29,28 +62,22 @@ module.exports = function(app) {
|
|||||||
req.on('error', stepCallback);
|
req.on('error', stepCallback);
|
||||||
},
|
},
|
||||||
function(err, body) {
|
function(err, body) {
|
||||||
|
req.body = body;
|
||||||
|
|
||||||
|
var key = req.method.toLowerCase() + ' ' + req.url,
|
||||||
|
handler = router.routes[key];
|
||||||
|
|
||||||
|
if (handler) {
|
||||||
|
handler(req, res, this.slot());
|
||||||
|
} else {
|
||||||
res.statusCode = 404;
|
res.statusCode = 404;
|
||||||
|
|
||||||
// run building of a project
|
|
||||||
if (req.url === '/api/builds' && req.method === 'POST') {
|
|
||||||
var projectName = body.project,
|
|
||||||
project = _(projects).findWhere({name: projectName});
|
|
||||||
|
|
||||||
if (project) {
|
|
||||||
res.statusCode = 204;
|
|
||||||
logger.log('Run project "%s"', projectName);
|
|
||||||
distributor.run({
|
|
||||||
projectName: projectName,
|
|
||||||
withScmChangesOnly: body.withScmChangesOnly,
|
|
||||||
initiator: {type: 'httpApi'}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
res.end();
|
res.end();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
function(err) {
|
function(err) {
|
||||||
logger.error('Error occurred during request: ', err.stack || err);
|
logger.error('Error occurred during request: ', err.stack || err);
|
||||||
|
res.statusCode = 500;
|
||||||
|
res.end();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user