mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-02-11 15:19:16 +00:00
use one http server for web interface and api
This commit is contained in:
parent
435ef01ad2
commit
c4031a4563
22
app.js
22
app.js
@ -14,10 +14,16 @@ var db = require('./db'),
|
|||||||
libLogger = require('./lib/logger'),
|
libLogger = require('./lib/logger'),
|
||||||
EventEmitter = require('events').EventEmitter;
|
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 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
|
// serve index for all app pages
|
||||||
if (req.url.indexOf('/data.io.js') === -1) {
|
if (req.url.indexOf('/data.io.js') === -1) {
|
||||||
if (!req.url.match(/(js|css|fonts)/)) {
|
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 socketio = require('socket.io')(server);
|
||||||
var dataio = require('./dataio')(socketio);
|
var dataio = require('./dataio')(socketio);
|
||||||
|
|
||||||
var app = new EventEmitter();
|
|
||||||
|
|
||||||
app.server = server;
|
app.server = server;
|
||||||
app.dataio = dataio;
|
app.dataio = dataio;
|
||||||
|
|
||||||
@ -113,7 +117,6 @@ Steppy(
|
|||||||
logger.log('Load plugin "%s"', plugin);
|
logger.log('Load plugin "%s"', plugin);
|
||||||
require(plugin).register(app);
|
require(plugin).register(app);
|
||||||
});
|
});
|
||||||
require('./httpApi').register(app);
|
|
||||||
|
|
||||||
notifier.init(app.config.notify, this.slot());
|
notifier.init(app.config.notify, this.slot());
|
||||||
|
|
||||||
@ -129,9 +132,14 @@ Steppy(
|
|||||||
// init resources
|
// init resources
|
||||||
require('./resources')(app);
|
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) {
|
function(err) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
app.server.listen(3000);
|
|
||||||
|
@ -14,6 +14,6 @@ notify:
|
|||||||
user: bot.nci@gmail.com
|
user: bot.nci@gmail.com
|
||||||
pass: pass
|
pass: pass
|
||||||
|
|
||||||
httpApi:
|
http:
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: 3030
|
port: 3000
|
||||||
|
21
httpApi.js
21
httpApi.js
@ -2,18 +2,17 @@
|
|||||||
|
|
||||||
var Steppy = require('twostep').Steppy,
|
var Steppy = require('twostep').Steppy,
|
||||||
_ = require('underscore'),
|
_ = require('underscore'),
|
||||||
http = require('http');
|
logger = require('./lib/logger')('http api');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Pure rest api on pure nodejs follows below
|
* Pure rest api on pure nodejs follows below
|
||||||
*/
|
*/
|
||||||
exports.register = function(app) {
|
module.exports = function(app) {
|
||||||
var config = _(app.config.httpApi).defaults({host: '127.0.0.1', port: 3030}),
|
return function(req, res) {
|
||||||
projects = app.projects,
|
|
||||||
distributor = app.distributor,
|
var projects = app.projects,
|
||||||
logger = app.lib.logger('http api');
|
distributor = app.distributor;
|
||||||
|
|
||||||
var server = http.createServer(function(req, res) {
|
|
||||||
Steppy(
|
Steppy(
|
||||||
function() {
|
function() {
|
||||||
var stepCallback = this.slot();
|
var stepCallback = this.slot();
|
||||||
@ -33,13 +32,13 @@ exports.register = function(app) {
|
|||||||
res.statusCode = 404;
|
res.statusCode = 404;
|
||||||
|
|
||||||
// run building of a project
|
// run building of a project
|
||||||
if (req.url === '/builds' && req.method === 'POST') {
|
if (req.url === '/api/builds' && req.method === 'POST') {
|
||||||
var projectName = body.project,
|
var projectName = body.project,
|
||||||
project = _(projects).findWhere({name: projectName});
|
project = _(projects).findWhere({name: projectName});
|
||||||
|
|
||||||
if (project) {
|
if (project) {
|
||||||
res.statusCode = 204;
|
res.statusCode = 204;
|
||||||
logger.log('Run "' + projectName + '"');
|
logger.log('Run project "%s"', projectName);
|
||||||
distributor.run({
|
distributor.run({
|
||||||
projectName: projectName,
|
projectName: projectName,
|
||||||
withScmChangesOnly: body.withScmChangesOnly,
|
withScmChangesOnly: body.withScmChangesOnly,
|
||||||
@ -54,8 +53,6 @@ exports.register = function(app) {
|
|||||||
logger.error('Error occurred during request: ', err.stack || err);
|
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);
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user