diff --git a/app.js b/app.js index e25c97f..f2bd8a2 100644 --- a/app.js +++ b/app.js @@ -17,8 +17,7 @@ var env = process.env.NODE_ENV || 'development', utils = require('./lib/utils'); var app = new EventEmitter(), - logger = libLogger('app'), - httpApi; + logger = libLogger('app'); var staticPath = path.join(__dirname, 'static'); @@ -56,14 +55,6 @@ app.httpServer.addRequestListener(function(req, res, next) { next(); }); -app.httpServer.addRequestListener(function(req, res, next) { - if (req.url.indexOf('/api/') === 0) { - return httpApi(req, res, next); - } else { - next(); - } -}); - var socketio = require('socket.io')(app.httpServer); var dataio = require('./dataio')(socketio); @@ -239,8 +230,6 @@ Steppy( require(plugin).register(app); }); - httpApi = require('./httpApi')(app); - notifier.init(app.config.notify, this.slot()); // init resources diff --git a/data/config.yaml b/data/config.yaml index af96f43..27b6d29 100644 --- a/data/config.yaml +++ b/data/config.yaml @@ -2,6 +2,7 @@ plugins: - nci-projects-reloader - nci-static-server + - nci-rest-api-server # - nci-mail-notification # - nci-jabber-notification # - nci-scheduler @@ -23,6 +24,7 @@ http: storage: backend: memdown + # backend: leveldown notify: mail: diff --git a/docs/developing-plugins/builds-collection.md b/docs/developing-plugins/builds-collection.md index 90e771c..e1d8cde 100644 --- a/docs/developing-plugins/builds-collection.md +++ b/docs/developing-plugins/builds-collection.md @@ -43,6 +43,7 @@ - `params.status` - optional status filter, can be used only when `params.projectName` is set. When used builds in the result will contain only following fields: id, number, startDate, endDate + - `params.limit` - maximum builds count to get ## BuildsCollection.getDoneStreak(params:Object, callback(err,doneStreak):Function) diff --git a/docs/developing-plugins/projects-collection.md b/docs/developing-plugins/projects-collection.md index f94e540..e7d80db 100644 --- a/docs/developing-plugins/projects-collection.md +++ b/docs/developing-plugins/projects-collection.md @@ -49,7 +49,7 @@ Remove project by name. Calls `unload`, removes project from disk and db. -## ProjectsCollection.rename(name:String, [callback(err)]:Function) +## ProjectsCollection.rename(name:String, newName:String, [callback(err)]:Function) Rename project. Renames project on disk and db, also changes name for loaded project. diff --git a/httpApi.js b/httpApi.js deleted file mode 100644 index 1f197c8..0000000 --- a/httpApi.js +++ /dev/null @@ -1,182 +0,0 @@ -'use strict'; - -var Steppy = require('twostep').Steppy, - _ = require('underscore'), - querystring = require('querystring'); -/* - * Pure rest api on pure nodejs follows below - */ - -var router = {}; -router.routes = {}; - -_(['get', 'post', 'patch', 'delete']).each(function(method) { - router[method] = function(path, handler) { - this.routes[method] = this.routes[method] || []; - var keys = [], - regExpStr = path.replace(/:(\w+)/g, function(match, name) { - keys.push(name); - return '(.+)'; - }); - - this.routes[method].push({ - regExp: new RegExp('^' + regExpStr + '$'), - handler: handler, - keys: keys - }); - }; -}); - -router.del = router['delete']; - -router.getRoute = function(req) { - var parts, - route = _(this.routes[req.method.toLowerCase()]).find(function(route) { - parts = route.regExp.exec(req.path); - return parts; - }); - - - if (route && route.keys.length) { - route.params = {}; - _(route.keys).each(function(key, index) { - route.params[key] = parts[index + 1]; - }); - } - - return route; -}; - -module.exports = function(app) { - var logger = app.lib.logger('http api'), - accessToken = (Math.random() * Math.random()).toString(36).substring(2); - - logger.log('access token is: %s', accessToken); - - // run building of a project - router.post('/api/0.1/builds', function(req, res, next) { - Steppy( - function() { - var projectName = req.body.project, - project = app.projects.get(projectName); - - if (project) { - res.statusCode = 204; - logger.log('Run project "%s"', projectName); - app.builds.create({ - projectName: projectName, - withScmChangesOnly: req.body.withScmChangesOnly, - queueQueued: req.body.queueQueued, - initiator: {type: 'httpApi'} - }); - } else { - res.statusCode = 404; - } - - res.end(); - }, - next - ); - }); - - router.del('/api/0.1/projects/:name', function(req, res, next) { - var token = req.body.token, - projectName = req.params.name; - - Steppy( - function() { - logger.log('Cleaning up project "%s"', projectName); - - if (token !== accessToken) { - throw new Error('Access token doesn`t match'); - } - - app.projects.remove(projectName, this.slot()); - }, - function() { - logger.log('Project "%s" cleaned up', projectName); - res.statusCode = 204; - res.end(); - }, - next - ); - }); - - router.patch('/api/0.1/projects/:name', function(req, res, next) { - var token = req.body.token, - projectName = req.params.name, - newProjectName = req.body.name; - - Steppy( - function() { - logger.log( - 'Rename project "%s" to "%s"', projectName, newProjectName - ); - - if (token !== accessToken) { - throw new Error('Access token doesn`t match'); - } - - if (!newProjectName) throw new Error('new project name is not set'); - - var curProject = app.projects.get(projectName); - if (!curProject) { - throw new Error('Project "' + projectName + '" not found'); - } - this.pass(curProject); - - var newProject = app.projects.get(newProjectName); - if (newProject) { - throw new Error( - 'Project name "' + newProjectName + '" already used' - ); - } - - app.projects.rename(projectName, newProjectName, this.slot()); - }, - function(err) { - res.statusCode = 204; - res.end(); - }, - next - ); - }); - - return function(req, res, next) { - - Steppy( - function() { - var stepCallback = this.slot(); - - var urlParts = req.url.split('?'); - req.path = urlParts[0]; - req.query = querystring.parse(urlParts[1]); - - req.setEncoding('utf-8'); - var bodyString = ''; - req.on('data', function(data) { - bodyString += data; - }); - req.on('end', function() { - var body = bodyString ? JSON.parse(bodyString) : {}; - stepCallback(null, body); - }); - req.on('error', stepCallback); - }, - function(err, body) { - req.body = body; - - var route = router.getRoute(req); - if (route) { - req.params = route.params; - route.handler(req, res, this.slot()); - } else { - res.statusCode = 404; - res.end(); - } - }, - next - ); - }; - -}; diff --git a/lib/build.js b/lib/build.js index 072d70f..b122f82 100644 --- a/lib/build.js +++ b/lib/build.js @@ -149,12 +149,12 @@ BuildsCollection.prototype.getAvgBuildDuration = function(builds) { * - `params.status` - optional status filter, can be used only when * `params.projectName` is set. When used builds in the result will contain * only following fields: id, number, startDate, endDate + * - `params.limit` - maximum builds count to get * * @param {Object} params * @param {Function} callback(err,builds) */ BuildsCollection.prototype.getRecent = function(params, callback) { - params.limit = params.limit || 20; var self = this; Steppy( diff --git a/lib/project.js b/lib/project.js index a69d877..c8b2b7d 100644 --- a/lib/project.js +++ b/lib/project.js @@ -290,6 +290,7 @@ ProjectsCollection.prototype.remove = function(name, callback) { * Renames project on disk and db, also changes name for loaded project. * * @param {String} name + * @param {String} newName * @param {Function} [callback(err)] */ ProjectsCollection.prototype.rename = function(name, newName, callback) { diff --git a/package.json b/package.json index d427c61..feb1dd3 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "memdown": "1.1.0", "mocha": "1.18.2", "nci-projects-reloader": "0.1.1", + "nci-rest-api-server": "0.1.1", "nci-static-server": "0.1.0", "nci-yaml-reader": "0.1.0", "nodemon": "1.3.7", diff --git a/resources/builds.js b/resources/builds.js index c7567c0..da5cbf1 100644 --- a/resources/builds.js +++ b/resources/builds.js @@ -11,7 +11,7 @@ module.exports = function(app) { Steppy( function() { var data = req.data || {}, - getParams = {limit: data.limit || 20}; + getParams = {limit: Number(data.limit) || 20}; if (data.projectName) { getParams.projectName = data.projectName;