-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp.js
119 lines (104 loc) · 3.54 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* 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: Hugues Malphettes.
*/
var fs = require('fs');
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://localhost: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(',');
}
if (process.env.OTHER_REVERSE_PROXIED) {
// for example for consul:
// {"/v1,/ui":{"host":"localhost","port":8500}}
try {
config.others = JSON.parse(process.env.OTHER_REVERSE_PROXIED);
} catch(x) {
console.error('Could not parse process.env.OTHER_REVERSE_PROXIED: ' + process.env.OTHER_REVERSE_PROXIED, x);
}
}
// 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.secure, config.es_port,
config.es_username, config.es_password, config.others);
var kibanaPath = 'kibana-build';
if (fs.existsSync('kibana/src')) {
kibanaPath = 'kibana/src';
} else if (fs.existsSync('kibana/index.html')) {
kibanaPath = 'kibana';
}
app.use('/', express.static(__dirname + '/' + kibanaPath));
server = app.listen(config.port, "0.0.0.0", function() {
console.log('server listening on ' + config.port);
});
// a basic healchckeck for a load balancer
app.get('/healthcheck', function(req, res){
res.json({ "status": "ok" });
});
}
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';
}
config.secure = secure;
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 = [
"define(['settings'],",
"function (Settings) {",
' "use strict";',
" return new Settings({",
' elasticsearch: "//" + window.location.hostname + ( window.location.port ? ":" + window.location.port : "") + "/__es",',
" default_route : '/dashboard/file/default.json',",
' kibana_index: "kibana-int",',
" panel_names: [ 'histogram', 'map', 'goal', 'table', 'filtering', 'timepicker', 'text', 'hits', 'column', 'trends', 'bettermap', 'query', 'terms', 'stats', 'sparklines' ]",
" });",
"});"
].join("\n");
response.setHeader('Content-Type', 'application/javascript');
response.end(responseBody);
}