From 8009d7e5848f69d65f9d666488fc93323f25f364 Mon Sep 17 00:00:00 2001 From: oleg Date: Sat, 9 Jan 2016 16:35:33 +0300 Subject: [PATCH] static server is plugin now --- app.js | 65 +++++++++++++++++------------------------------- data/config.yaml | 7 ++++++ lib/utils.js | 10 ++++++++ package.json | 8 +++--- 4 files changed, 44 insertions(+), 46 deletions(-) diff --git a/app.js b/app.js index 9e929f9..e25c97f 100644 --- a/app.js +++ b/app.js @@ -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')); diff --git a/data/config.yaml b/data/config.yaml index 4d839af..af96f43 100644 --- a/data/config.yaml +++ b/data/config.yaml @@ -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 diff --git a/lib/utils.js b/lib/utils.js index 352db2d..122ffcf 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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); +}; diff --git a/package.json b/package.json index 80973b1..d427c61 100644 --- a/package.json +++ b/package.json @@ -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",