Skip to content

Commit

Permalink
Merge pull request #2 from w33ble/dev-server
Browse files Browse the repository at this point in the history
Dev server setup
  • Loading branch information
simianhacker committed Jan 21, 2015
2 parents 35ac728 + 39034a1 commit 4f82813
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var requestLogger = require('./lib/requestLogger');
var appHeaders = require('./lib/appHeaders');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compression = require('compression');
Expand All @@ -15,9 +16,11 @@ var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('x-powered-by', false);

app.use(favicon(path.join(config.public_folder, 'styles', 'theme', 'elk.ico')));
app.use(requestLogger());
app.use(appHeaders());

if (app.get('env') === 'development') {
require('./dev')(app);
Expand Down
11 changes: 11 additions & 0 deletions src/server/lib/appHeaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var path = require('path');
var pkgPath = path.resolve(__dirname, '..', '..', '..', 'package.json');
var pkg = require(pkgPath);

module.exports = function () {
return function (req, res, next) {
res.header('X-App-Name', 'kibana');
res.header('X-App-Version', pkg.version);
next();
};
};
2 changes: 2 additions & 0 deletions tasks/kibana_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module.exports = function (grunt) {
var config = require('../src/server/config');
config.quiet = true;
var server = require('../src/server');

server.start(function () {
grunt.log.ok('Server started on port', config.kibana.port);
if (keepalive !== 'keepalive') done();
});
});
Expand Down
30 changes: 22 additions & 8 deletions tasks/maybe_connect_dev.js → tasks/maybe_start_kibana.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
module.exports = function (grunt) {
var config = require('../src/server/config');

var maybeStartServer = function (options) {
return function () {
var http = require('http');

var req = http.request({
var opts = {
method: 'HEAD',
path: '/',
host: 'localhost',
port: options.port
});
};

grunt.log.debug('checking for server', JSON.stringify(opts));

var req = http.request(opts);

function onResponse(res) {
if (res.statusCode === 200) {
grunt.log.writeln('server already started');
grunt.log.debug('Server responded with', res.statusCode);
var app = res.headers['x-app-name'];

if (res.statusCode === 200 && app && app === 'kibana') {
grunt.log.ok('Kibana server already started on port', options.port);
} else {
grunt.log.error('another server is already running at localhost:8000!');
grunt.log.error('Another server is already running on port', options.port);
process.exit(1);
}
done(res);
}

function onError() {
function onError(err) {
if (err.code !== 'ECONNREFUSED') {
grunt.log.error('Kibana server check failed', err);
}

grunt.config.set(options.name, true);
grunt.task.run(options.tasks);
done();
Expand All @@ -39,9 +52,10 @@ module.exports = function (grunt) {
req.end();
};
};

grunt.registerTask('maybe_start_kibana', maybeStartServer({
name: 'kibana-server',
port: 5601,
port: config.kibana.port,
tasks: ['kibana_server']
}));
};

0 comments on commit 4f82813

Please sign in to comment.