<commit_before>/**
 * Hosts kibana as an express application.
 * Optional google oauth integration.
 * Proxies the calls to elasticsearch
 * Insert basic authentication to elasticsearch calls if necessary.
 * Deployable on cloudfoundry.
 *
 * License: MIT
 * Copyright: Sutoiku, Inc.
 * Author: <NAME>.
 */

var parseUrl = require('url').parse;
var express = require('express');
var app = express();
var server;

var configureESProxy = require('./lib/es-proxy').configureESProxy;
var configureOAuth = require('./lib/google-oauth').configureOAuth;

var config = createConfig();
configureApp(app, config);

function createConfig() {
  var config = {};
  if (!config.port) {
    config.port = process.env.PORT || process.env.VCAP_APP_PORT || 3003;
  }
  if (!config.es_url) {
    config.es_url = process.env.ES_URL || 'http://hugues.local:9200';
  }
  // oauth
  if (process.env.APP_ID && process.env.APP_SECRET) {
    config.appID = process.env.APP_ID;
    config.appSecret = process.env.APP_SECRET;
    config.authorizedEmails = (process.env.AUTHORIZED_EMAILS || '*').split(',');
  }
  // parse the url
  parseESURL(config.es_url, config);

  return config;
}

function configureApp(app, config) {
  app.disable('x-powered-by');
  app.use(express.logger('dev'));

  configureOAuth(express, app, config);

  app.get('/config.js', kibanaConfig);

  configureESProxy(app, config.es_host, config.es_port,
            config.es_username, config.es_password);

  app.use('/', express.static(__dirname + '/kibana'));
  server = app.listen(config.port, /*"0.0.0.0",*/ function() {
    console.log('server listening on ' + config.port);
  });
}

function parseESURL(esurl, config) {
  var urlP = parseUrl(esurl);
  config.es_host = urlP.hostname;

  var secure = urlP.protocol === 'https:';
  if (urlP.port !== null && urlP.port !== undefined) {
      config.es_port = urlP.port;
  } else if (secure) {
      config.es_port = '443';
  } else {
      config.es_port = '80';
  }
  if (urlP.auth) {
    var toks = urlP.auth.split(':');
    config.es_username = toks[0];
    config.es_password = toks[1] || '';
  }  
}

function kibanaConfig(request, response) {
  //returns the javascript that is the config object. for kibana.
  var responseBody = "var config = new Settings({\n" +
  "elasticsearch: '//'+window.location.host+'/__es',\n" +
  "kibana_index: 'kibana-int',\n" + 
  "modules: ['histogram','map','pie','table','stringquery','sort'," +
            "'timepicker','text','fields','hits','dashcontrol','column','derivequeries','trends']\n" +
  "});";
  response.setHeader('Content-Type', 'application/javascript');
  response.end(responseBody);
}
<commit_msg>keep up with the latest kibana<commit_after>/**
 * Hosts kibana as an express application.
 * Optional google oauth integration.
 * Proxies the calls to elasticsearch
 * Insert basic authentication to elasticsearch calls if necessary.
 * Deployable on cloudfoundry.
 *
 * License: MIT
 * Copyright: Sutoiku, Inc.
 * Author: <NAME>.
 */

var parseUrl = require('url').parse;
var express = require('express');
var app = express();
var server;

var configureESProxy = require('./lib/es-proxy').configureESProxy;
var configureOAuth = require('./lib/google-oauth').configureOAuth;

var config = createConfig();
configureApp(app, config);

function createConfig() {
  var config = {};
  if (!config.port) {
    config.port = process.env.PORT || process.env.VCAP_APP_PORT || 3003;
  }
  if (!config.es_url) {
    config.es_url = process.env.ES_URL || 'http://hugues.local:9200';
  }
  // oauth
  if (process.env.APP_ID && process.env.APP_SECRET) {
    config.appID = process.env.APP_ID;
    config.appSecret = process.env.APP_SECRET;
    config.authorizedEmails = (process.env.AUTHORIZED_EMAILS || '*').split(',');
  }
  // parse the url
  parseESURL(config.es_url, config);

  return config;
}

function configureApp(app, config) {
  app.disable('x-powered-by');
  app.use(express.logger('dev'));

  configureOAuth(express, app, config);

  app.get('/config.js', kibanaConfig);

  configureESProxy(app, config.es_host, config.es_port,
            config.es_username, config.es_password);

  app.use('/', express.static(__dirname + '/kibana'));
  server = app.listen(config.port, /*"0.0.0.0",*/ function() {
    console.log('server listening on ' + config.port);
  });
}

function parseESURL(esurl, config) {
  var urlP = parseUrl(esurl);
  config.es_host = urlP.hostname;

  var secure = urlP.protocol === 'https:';
  if (urlP.port !== null && urlP.port !== undefined) {
      config.es_port = urlP.port;
  } else if (secure) {
      config.es_port = '443';
  } else {
      config.es_port = '80';
  }
  if (urlP.auth) {
    var toks = urlP.auth.split(':');
    config.es_username = toks[0];
    config.es_password = toks[1] || '';
  }  
}

function kibanaConfig(request, response) {
  //returns the javascript that is the config object. for kibana.
  var responseBody = "var config = new Settings({\n" +
  "elasticsearch: '//'+window.location.host+'/__es',\n" +
  "kibana_index: 'kibana-int',\n" + 
  "modules: ['histogram','map','pie','table','filtering'," +
            "'timepicker','text','fields','hits','dashcontrol',"+
            "'column','derivequeries','trends','bettermap','query']\n" +
  "});";
  response.setHeader('Content-Type', 'application/javascript');
  response.end(responseBody);
}

{'fraction_non_alphanumeric': 0.172954800149421, 'fraction_numerical': 0.007097497198356369, 'mean_word_length': 3.828674481514878, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6209257', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1808, 'n_words': 366}