static server is plugin now

This commit is contained in:
oleg 2016-01-09 16:35:33 +03:00
parent 0824f3bbad
commit 8009d7e584
4 changed files with 44 additions and 46 deletions

65
app.js
View File

@ -3,7 +3,6 @@
var env = process.env.NODE_ENV || 'development',
db = require('./db'),
httpServer = require('./lib/httpServer'),
nodeStatic = require('node-static'),
path = require('path'),
fs = require('fs'),
Steppy = require('twostep').Steppy,
@ -14,15 +13,14 @@ var env = process.env.NODE_ENV || 'development',
BuildsCollection = require('./lib/build').BuildsCollection,
libLogger = require('./lib/logger'),
EventEmitter = require('events').EventEmitter,
validateConfig = require('./lib/validateConfig');
validateConfig = require('./lib/validateConfig'),
utils = require('./lib/utils');
var app = new EventEmitter(),
logger = libLogger('app'),
httpApi;
var staticPath = path.join(__dirname, 'static'),
staticServer = new nodeStatic.Server(staticPath),
staticDataServer;
var staticPath = path.join(__dirname, 'static');
var httpServerLogger = libLogger('http server');
@ -66,41 +64,6 @@ app.httpServer.addRequestListener(function(req, res, next) {
}
});
app.httpServer.addRequestListener(function(req, res, next) {
if (new RegExp('^/projects/(\\w|-)+/workspace/').test(req.url)) {
return staticDataServer.serve(req, res);
} else {
next();
}
});
app.httpServer.addRequestListener(function(req, res, next) {
if (new RegExp('^/(js|css|fonts|images)/').test(req.url)) {
staticServer.serve(req, res);
} else {
next();
}
});
app.httpServer.addRequestListener(function(req, res, next) {
if (req.url.indexOf('/data.io.js') === -1) {
// serve index for all app pages
if (env === 'development') {
var jade = require('jade');
// Compile a function
var index = jade.compileFile(__dirname + '/views/index.jade');
res.write(index({env: env}));
res.end();
} else {
// serve index for all other pages (/builds/:id, etc)
fs.createReadStream(path.join(staticPath, 'index.html'))
.pipe(res);
}
} else {
next();
}
});
var socketio = require('socket.io')(app.httpServer);
var dataio = require('./dataio')(socketio);
@ -196,7 +159,6 @@ Steppy(
// path to root dir (with projects, builds etc)
app.config.paths.data = path.join(process.cwd(), 'data');
staticDataServer = new nodeStatic.Server(app.config.paths.data);
app.config.paths.projects = path.join(app.config.paths.data, 'projects');
app.config.paths.db = path.join(app.config.paths.data, 'db');
@ -239,7 +201,7 @@ Steppy(
_(app.config).defaults(config);
_(app.config).defaults(configDefaults);
logger.log('Server config:', JSON.stringify(app.config, null, 4));
logger.log('Server config:', utils.toPrettyJson(app.config));
var dbBackend = require(app.config.storage.backend);
@ -288,6 +250,25 @@ Steppy(
// load projects after all plugins to provide ability for plugins to
// handle `projectLoaded` event
app.projects.loadAll(this.slot());
// serve index for all app pages, add this listener after all other
// listeners
app.httpServer.addRequestListener(function(req, res, next) {
if (req.url.indexOf('/data.io.js') === -1) {
if (env === 'development') {
var jade = require('jade');
// Compile a function
var index = jade.compileFile(__dirname + '/views/index.jade');
res.write(index({env: env}));
res.end();
} else {
fs.createReadStream(path.join(staticPath, 'index.html'))
.pipe(res);
}
} else {
next();
}
});
},
function(err) {
logger.log('Loaded projects: ', _(app.projects.getAll()).pluck('name'));

View File

@ -1,6 +1,7 @@
plugins:
- nci-projects-reloader
- nci-static-server
# - nci-mail-notification
# - nci-jabber-notification
# - nci-scheduler
@ -13,6 +14,12 @@ http:
host: 127.0.0.1
port: 3000
url: http://127.0.0.1:3000
static:
locations:
- url: !!js/regexp ^/(js|css|fonts|images)/
root: static/
- url: !!js/regexp ^/projects/(\w|-)+/workspace/
root: data/
storage:
backend: memdown

View File

@ -14,3 +14,13 @@ exports.lpad = function(str, length, chr) {
exports.toNumberStr = function(number) {
return exports.lpad(String(number), 20);
};
exports.toPrettyJson = function(data) {
return JSON.stringify(data, function(key, value) {
if (_(value).isRegExp()) {
return 'RegExp ' + String(value);
} else {
return value;
}
}, 4);
};

View File

@ -46,14 +46,13 @@
"homepage": "https://github.com/node-ci/nci",
"dependencies": {
"colors": "1.1.2",
"conform": "0.2.12",
"data.io": "0.3.0",
"nlevel": "1.0.3",
"node-static": "0.7.6",
"socket.io": "1.3.5",
"twostep": "0.4.1",
"underscore": "1.8.3",
"through": "2.3.6",
"conform": "0.2.12"
"twostep": "0.4.1",
"underscore": "1.8.3"
},
"devDependencies": {
"bower": "1.4.1",
@ -68,6 +67,7 @@
"memdown": "1.1.0",
"mocha": "1.18.2",
"nci-projects-reloader": "0.1.1",
"nci-static-server": "0.1.0",
"nci-yaml-reader": "0.1.0",
"nodemon": "1.3.7",
"nrun": "0.1.4",