From c4031a45636609016d9ac0e3c8f23b32fa94cc30 Mon Sep 17 00:00:00 2001 From: oleg Date: Sun, 12 Jul 2015 15:05:33 +0300 Subject: [PATCH] use one http server for web interface and api --- app.js | 22 +++++++++++++++------- data/config.yaml | 4 ++-- httpApi.js | 21 +++++++++------------ 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/app.js b/app.js index 6f587ae..23d5cd7 100644 --- a/app.js +++ b/app.js @@ -14,10 +14,16 @@ var db = require('./db'), libLogger = require('./lib/logger'), EventEmitter = require('events').EventEmitter; -var logger = libLogger('app'); +var app = new EventEmitter(), + logger = libLogger('app'), + httpApi = require('./httpApi')(app); var staticServer = new nodeStatic.Server('./static'); -var server = http.createServer(function(req, res, next) { +var server = http.createServer(function(req, res) { + if (req.url.indexOf('/api/') === 0) { + return httpApi(req, res); + } + // serve index for all app pages if (req.url.indexOf('/data.io.js') === -1) { if (!req.url.match(/(js|css|fonts)/)) { @@ -34,8 +40,6 @@ var server = http.createServer(function(req, res, next) { var socketio = require('socket.io')(server); var dataio = require('./dataio')(socketio); -var app = new EventEmitter(); - app.server = server; app.dataio = dataio; @@ -113,7 +117,6 @@ Steppy( logger.log('Load plugin "%s"', plugin); require(plugin).register(app); }); - require('./httpApi').register(app); notifier.init(app.config.notify, this.slot()); @@ -129,9 +132,14 @@ Steppy( // init resources require('./resources')(app); }, + function(err) { + var httpConfig = _(app.config.http).defaults({ + host: '127.0.0.1', port: 3000 + }); + logger.log('Start http server on %s:%s', httpConfig.host, httpConfig.port); + app.server.listen(httpConfig.port, httpConfig.host); + }, function(err) { if (err) throw err; } ); - -app.server.listen(3000); diff --git a/data/config.yaml b/data/config.yaml index b1ac4f0..b792827 100644 --- a/data/config.yaml +++ b/data/config.yaml @@ -14,6 +14,6 @@ notify: user: bot.nci@gmail.com pass: pass -httpApi: +http: host: 127.0.0.1 - port: 3030 + port: 3000 diff --git a/httpApi.js b/httpApi.js index 6800a27..dda89ef 100644 --- a/httpApi.js +++ b/httpApi.js @@ -2,18 +2,17 @@ var Steppy = require('twostep').Steppy, _ = require('underscore'), - http = require('http'); + logger = require('./lib/logger')('http api'); /* * Pure rest api on pure nodejs follows below */ -exports.register = function(app) { - var config = _(app.config.httpApi).defaults({host: '127.0.0.1', port: 3030}), - projects = app.projects, - distributor = app.distributor, - logger = app.lib.logger('http api'); +module.exports = function(app) { + return function(req, res) { + + var projects = app.projects, + distributor = app.distributor; - var server = http.createServer(function(req, res) { Steppy( function() { var stepCallback = this.slot(); @@ -33,13 +32,13 @@ exports.register = function(app) { res.statusCode = 404; // run building of a project - if (req.url === '/builds' && req.method === 'POST') { + 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 "' + projectName + '"'); + logger.log('Run project "%s"', projectName); distributor.run({ projectName: projectName, withScmChangesOnly: body.withScmChangesOnly, @@ -54,8 +53,6 @@ exports.register = function(app) { logger.error('Error occurred during request: ', err.stack || err); } ); - }); + }; - logger.log('Start listenning on %s:%s', config.host, config.port); - server.listen(config.port, config.host); };