use one http server for web interface and api

This commit is contained in:
oleg 2015-07-12 15:05:33 +03:00
parent 435ef01ad2
commit c4031a4563
3 changed files with 26 additions and 21 deletions

22
app.js
View File

@ -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);

View File

@ -14,6 +14,6 @@ notify:
user: bot.nci@gmail.com
pass: pass
httpApi:
http:
host: 127.0.0.1
port: 3030
port: 3000

View File

@ -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);
};