From 94ff1e9ead4a440456ae5593adf6d611e26fff65 Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Tue, 20 Jan 2015 11:15:31 -0700 Subject: [PATCH] removed the dev server and cleaned up package.json --- package.json | 18 +-- src/server/app.js | 4 + .../server/dev}/_amd_rapper.js | 0 .../server/dev}/_instrumentation.js | 0 src/server/dev/index.js | 56 +++++++ tasks/dev.js | 1 - tasks/maybe_connect_dev.js | 5 - tasks/ruby_server.js | 34 ---- tasks/server.js | 15 -- tasks/test.js | 4 - test/utils/dev_server/index.js | 145 ------------------ 11 files changed, 63 insertions(+), 219 deletions(-) rename {test/utils/dev_server => src/server/dev}/_amd_rapper.js (100%) rename {test/utils/dev_server => src/server/dev}/_instrumentation.js (100%) create mode 100644 src/server/dev/index.js delete mode 100644 tasks/ruby_server.js delete mode 100644 tasks/server.js delete mode 100644 test/utils/dev_server/index.js diff --git a/package.json b/package.json index 0e313bc79a6f7..928a48fb35e69 100644 --- a/package.json +++ b/package.json @@ -24,11 +24,10 @@ "Lukas Olson " ], "scripts": { + "test": "grunt test", "start": "node ./src/server/bin/kibana.js", - "test": "grunt test --use-jruby", - "server": "grunt server", - "precommit": "grunt hintStagedFiles", - "prepush": "echo" + "server": "node ./src/server/bin/kibana.js", + "precommit": "grunt hintStagedFiles" }, "repository": { "type": "git", @@ -93,17 +92,6 @@ "simple-git": "^0.11.0", "tar": "^1.0.1" }, - "scripts": { - "test": "grunt test --use-jruby", - "server": "grunt server", - "precommit": "grunt hintStagedFiles", - "prepush": "echo" - }, - "repository": { - "type": "git", - "url": "git@github.com:elasticsearch/kibana.git" - }, - "author": "", "license": "Apache 2.0", "bugs": { "url": "https://github.com/elasticsearch/kibana/issues" diff --git a/src/server/app.js b/src/server/app.js index b1325dd47dc75..2f3d069131414 100644 --- a/src/server/app.js +++ b/src/server/app.js @@ -19,6 +19,10 @@ app.set('view engine', 'jade'); app.use(favicon(path.join(config.public_folder, 'styles', 'theme', 'elk.ico'))); app.use(requestLogger()); +if (app.get('env') === 'development') { + require('./dev')(app); +} + // The proxy must be set up before all the other middleware. // TODO: WE might want to move the middleware to each of the individual routes // so we don't have weird conflicts in the future. diff --git a/test/utils/dev_server/_amd_rapper.js b/src/server/dev/_amd_rapper.js similarity index 100% rename from test/utils/dev_server/_amd_rapper.js rename to src/server/dev/_amd_rapper.js diff --git a/test/utils/dev_server/_instrumentation.js b/src/server/dev/_instrumentation.js similarity index 100% rename from test/utils/dev_server/_instrumentation.js rename to src/server/dev/_instrumentation.js diff --git a/src/server/dev/index.js b/src/server/dev/index.js new file mode 100644 index 0000000000000..f6dd976e4cb30 --- /dev/null +++ b/src/server/dev/index.js @@ -0,0 +1,56 @@ +/* jshint node:true */ + +var express = require('express'); +var instrumentationMiddleware = require('./_instrumentation'); +var amdRapperMiddleware = require('./_amd_rapper'); + +var glob = require('glob'); +var path = require('path'); +var join = path.join; +var rel = join.bind(null, __dirname); +var ROOT = rel('../../../'); +var SRC = join(ROOT, 'src'); +var APP = join(SRC, 'kibana'); +var TEST = join(ROOT, 'test'); + +module.exports = function (app) { + app.use(instrumentationMiddleware({ + root: SRC, + displayRoot: SRC, + filter: function (filename) { + return filename.match(/.*\/src\/.*\.js$/) + && !filename.match(/.*\/src\/kibana\/bower_components\/.*\.js$/) + && !filename.match(/.*\/src\/kibana\/utils\/(event_emitter|next_tick|rison)\.js$/); + } + })); + + app.use(instrumentationMiddleware({ + root: APP, + displayRoot: SRC, + filter: function (filename) { + return filename.match(/.*\/src\/.*\.js$/) + && !filename.match(/.*\/src\/kibana\/bower_components\/.*\.js$/) + && !filename.match(/.*\/src\/kibana\/utils\/(event_emitter|next_tick|rison)\.js$/); + } + })); + + app.use(amdRapperMiddleware({ + root: ROOT + })); + + app.use('/test', express.static(TEST)); + app.use('/specs', function (req, res) { + var unit = join(ROOT, '/test/unit/'); + glob(join(unit, 'specs/**/*.js'), function (er, files) { + var moduleIds = files + .filter(function (filename) { + return path.basename(filename).charAt(0) !== '_'; + }) + .map(function (filename) { + return path.relative(unit, filename).replace(/\.js$/, ''); + }); + + res.end(JSON.stringify(moduleIds)); + }); + }); +}; diff --git a/tasks/dev.js b/tasks/dev.js index d3082d583a869..2167bc6804e32 100644 --- a/tasks/dev.js +++ b/tasks/dev.js @@ -7,7 +7,6 @@ module.exports = function (grunt) { 'jade', 'esvm:dev', 'maybe_start_kibana', - 'maybe_start_server', 'watch' ]; diff --git a/tasks/maybe_connect_dev.js b/tasks/maybe_connect_dev.js index 34517a4003bf9..8e44084a73217 100644 --- a/tasks/maybe_connect_dev.js +++ b/tasks/maybe_connect_dev.js @@ -39,11 +39,6 @@ module.exports = function (grunt) { req.end(); }; }; - grunt.registerTask('maybe_start_server', maybeStartServer({ - name: 'connect-server', - port: 8000, - tasks: ['server'] - })); grunt.registerTask('maybe_start_kibana', maybeStartServer({ name: 'kibana-server', port: 5601, diff --git a/tasks/ruby_server.js b/tasks/ruby_server.js deleted file mode 100644 index bf8d576036c9e..0000000000000 --- a/tasks/ruby_server.js +++ /dev/null @@ -1,34 +0,0 @@ -var request = require('request'); -module.exports = function (grunt) { - grunt.registerTask('ruby_server', function () { - var done = this.async(); - - request.get('http://localhost:5601/config', function (err, resp, body) { - // err is a failed response, no server is running - if (err) { - // run mri_server by default - var tasks = ['run:mri_server']; - grunt.config.set('ruby_server', 'mri_server'); - - // if jruby flag is set, use jruby - if (grunt.option('use-jruby')) { - tasks = [ - 'download_jruby', - 'install_gems', - 'run:jruby_server', - 'wait_for_jruby' - ]; - grunt.config.set('ruby_server', 'jruby_server'); - } - - grunt.task.run(tasks); - - // response means server is already running - } else { - grunt.log.error('Another ruby server is running on localhost:5601.'); - } - - done(); - }); - }); -}; diff --git a/tasks/server.js b/tasks/server.js deleted file mode 100644 index f6a38682f7320..0000000000000 --- a/tasks/server.js +++ /dev/null @@ -1,15 +0,0 @@ -module.exports = function (grunt) { - grunt.registerTask('server', function (keepalive) { - var done = this.async(); - var DevServer = require('../test/utils/dev_server'); - var server = new DevServer(); - - server.listen(8000).then(function () { - console.log('visit http://localhost:8000'); - - if (keepalive !== 'keepalive') { - done(); - } - }); - }); -}; diff --git a/tasks/test.js b/tasks/test.js index 13e9dcf92ae17..1d16b7bd4bf75 100644 --- a/tasks/test.js +++ b/tasks/test.js @@ -25,7 +25,6 @@ module.exports = function (grunt) { var tasks = [ 'jshint', 'maybe_start_kibana', - 'maybe_start_server', 'jade', 'less', getTestTask() @@ -36,7 +35,6 @@ module.exports = function (grunt) { grunt.registerTask('quick-test', function () { var tasks = [ 'maybe_start_kibana', - 'maybe_start_server', getTestTask() ]; grunt.task.run(tasks); @@ -45,13 +43,11 @@ module.exports = function (grunt) { grunt.registerTask('coverage', [ 'blanket', 'maybe_start_kibana', - 'maybe_start_server', 'mocha:coverage' ]); grunt.registerTask('test:watch', [ 'maybe_start_kibana', - 'maybe_start_server', 'watch:test' ]); }; diff --git a/test/utils/dev_server/index.js b/test/utils/dev_server/index.js deleted file mode 100644 index ac04166a4a18a..0000000000000 --- a/test/utils/dev_server/index.js +++ /dev/null @@ -1,145 +0,0 @@ -/* jshint node:true */ - -var connect = require('connect'); -var http = require('http'); -var Promise = require('bluebird'); - -var instrumentationMiddleware = require('./_instrumentation'); -var amdRapperMiddleware = require('./_amd_rapper'); -var proxy = require('http-proxy').createProxyServer({}); - -var glob = require('glob'); -var path = require('path'); -var join = path.join; -var rel = join.bind(null, __dirname); -var ROOT = rel('../../../'); -var SRC = join(ROOT, 'src'); -var APP = join(SRC, 'kibana'); -var TEST = join(ROOT, 'test'); -var PLUGINS = join(SRC, 'plugins'); - -module.exports = function DevServer(opts) { - opts = opts || {}; - - var server = this; - var app = connect(); - var httpServer = http.createServer(app); - - // Kibana Backend Proxy - app.use(function (req, res, next) { - // Proxy config and es requests to the Kibana Backend - if (/^\/(config|elasticsearch\/)/.test(req.url)) { - return proxy.web(req, res, { target: 'http://localhost:5601' }); - } - - next(); - }); - - app.use(instrumentationMiddleware({ - root: SRC, - displayRoot: SRC, - filter: function (filename) { - return filename.match(/.*\/src\/.*\.js$/) - && !filename.match(/.*\/src\/kibana\/bower_components\/.*\.js$/) - && !filename.match(/.*\/src\/kibana\/utils\/(event_emitter|next_tick|rison)\.js$/); - } - })); - - app.use(instrumentationMiddleware({ - root: APP, - displayRoot: SRC, - filter: function (filename) { - return filename.match(/.*\/src\/.*\.js$/) - && !filename.match(/.*\/src\/kibana\/bower_components\/.*\.js$/) - && !filename.match(/.*\/src\/kibana\/utils\/(event_emitter|next_tick|rison)\.js$/); - } - })); - - app.use(amdRapperMiddleware({ - root: ROOT - })); - - app.use(connect.static(ROOT)); - app.use(connect.static(APP)); - app.use('/test', connect.static(TEST)); - app.use('/plugins', connect.static(PLUGINS)); - - app.use('/specs', function (req, res) { - var unit = join(ROOT, '/test/unit/'); - glob(join(unit, 'specs/**/*.js'), function (er, files) { - var moduleIds = files - .filter(function (filename) { - return path.basename(filename).charAt(0) !== '_'; - }) - .map(function (filename) { - return path.relative(unit, filename).replace(/\.js$/, ''); - }); - - res.end(JSON.stringify(moduleIds)); - }); - }); - - // respond to the "maybe_start_server" pings - app.use(function (req, res, next) { - if (req.method !== 'HEAD' || req.url !== '/') return next(); - res.statusCode === 200; - res.setHeader('Pong', 'Kibana 4 Dev Server'); - res.end(); - }); - - app.use(function (req, res, next) { - if (req.url !== '/') return next(); - res.statusCode = 303; - res.setHeader('Location', '/src/'); - res.end(); - }); - - // prevent chrome's stupid "this page is in spanish" on the directories page - app.use(function (req, res, next) { - res.setHeader('Content-Language', 'en'); - next(); - }); - - // allow browsing directories - app.use(connect.directory(ROOT)); - - server.listenOnFirstOpenPort = function (ports) { - var options = ports.slice(0); - - // wrap this logic in an IIFE so that we can call it again later - return (function attempt() { - var port = options.shift(); - if (!port) return Promise.reject(new Error('None of the supplied options succeeded')); - - return server.listen(port) - // filter out EADDRINUSE errors and call attempt again - .catch(function (err) { - if (err.code === 'EADDRINUSE') return attempt(); - throw err; - }); - })(); - }; - - server.listen = function (port) { - return new Promise(function (resolve, reject) { - var done = function (err) { - httpServer.removeListener('error', done); - httpServer.removeListener('listening', done); - - // pass the error along - if (err) return reject(err); - - resolve(server.port = httpServer.address().port); - }; - - // call done with an error - httpServer.on('error', done, true); - // call done without any args - httpServer.on('listening', done, true); - - httpServer.listen(port); - }); - }; - - server.close = httpServer.close.bind(httpServer); -};