<commit_before>/**
 * Hosts the latest grafana and elasticsearch behind Google OAuth2 Authentication
 * with nodejs and express.
 * License: MIT
 * Copyright: Funplus Game Inc.
 * Author: strima
 * Original Author: <NAME>.
 * Project: https://github.com/strima/grafana-authentication-proxy
 */

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

var config = require('./config');
var app = express();

app.use(express.logger());

console.log('Server starting...');

if (!config.base_path) {
	config.base_path="";
	console.log("No base_path specified in config so using /");
}

app.use(express.cookieParser());
app.use(express.session({ secret: config.cookie_secret }));

// Authentication
if (config.enable_basic_auth && config.basic_auth_file && fs.existsSync(config.basic_auth_file)) {
    console.log('basic_auth_file defined and found, so reading it ...');
    config.basic_auth_users=new Array();
    var basic_auth_users=fs.readFileSync(config.basic_auth_file,'utf8');
    var userpass=basic_auth_users.split('\n');
    for (var userpass_index in userpass) {
        var uspa=userpass[userpass_index].match(/^([^:]+):(.+)/);
        if (uspa) {
            config.basic_auth_users[config.basic_auth_users.length]={"user": uspa[1], "password": uspa[2]};
        }
    }
}
require('./lib/basic-auth').configureBasic(express, app, config);
require('./lib/google-oauth').configureOAuth(express, app, config);
require('./lib/cas-auth.js').configureCas(express, app, config);

// Setup ES proxy
require('./lib/es-proxy').configureESProxy(app, config.es_host, config.es_port,
          config.es_username, config.es_password, config.base_path);

// Setup Graphite proxy
require('./lib/gr-proxy').configureGRProxy(app, config.gr_host, config.gr_port,
          config.gr_username, config.gr_password, config.base_path);

// Serve config.js for grafana
// We should use special config.js for the frontend and point the ES to __es/
app.get(config.base_path + '/config.js', grafanaconfigjs);

// Serve all grafana frontend files
app.use(express.compress());
app.use(config.base_path + '/', express.static(__dirname + '/grafana/src', {maxAge: config.brower_cache_maxage || 0}));


run();

function run() {
  if (config.enable_ssl_port === true) {
    var options = {
      key: fs.readFileSync(config.ssl_key_file),
      cert: fs.readFileSync(config.ssl_cert_file),
    };<commit_msg>* added filewatch for basic auth file, so that no restart is necessary on change of this file<commit_after>/**
 * Hosts the latest grafana and elasticsearch behind Google OAuth2 Authentication
 * with nodejs and express.
 * License: MIT
 * Copyright: Funplus Game Inc.
 * Author: strima
 * Original Author: <NAME>.
 * Project: https://github.com/strima/grafana-authentication-proxy
 */

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

var config = require('./config');
var app = express();

app.use(express.logger());

console.log('Server starting...');

if (!config.base_path) {
	config.base_path="";
	console.log("No base_path specified in config so using /");
}

app.use(express.cookieParser());
app.use(express.session({ secret: config.cookie_secret }));

// Authentication
function readAndInitBasicAuthFile() {
    config.basic_auth_users=new Array();
    var basic_auth_users=fs.readFileSync(config.basic_auth_file,'utf8');
    var userpass=basic_auth_users.split('\n');
    for (var userpass_index in userpass) {
        var uspa=userpass[userpass_index].match(/^([^:]+):(.+)/);
        if (uspa) {
            config.basic_auth_users[config.basic_auth_users.length]={"user": uspa[1], "password": uspa[2]};
        }
    }
}
if (config.enable_basic_auth && config.basic_auth_file && fs.existsSync(config.basic_auth_file)) {
    console.log('basic_auth_file defined and found, so reading it ...');
    readAndInitBasicAuthFile();
    fs.watchFile(config.basic_auth_file, { persistent: true, interval: 5007 }, function(curr,prev) {
        if (curr.mtime.getTime() != prev.mtime.getTime()) {
            console.log('BASIC AUTH File was changed, so reloading values');
            readAndInitBasicAuthFile();
        }
    });
}

require('./lib/basic-auth').configureBasic(express, app, config);
require('./lib/google-oauth').configureOAuth(express, app, config);
require('./lib/cas-auth.js').configureCas(express, app, config);

// Setup ES proxy
require('./lib/es-proxy').configureESProxy(app, config.es_host, config.es_port,
          config.es_username, config.es_password, config.base_path);

// Setup Graphite proxy
require('./lib/gr-proxy').configureGRProxy(app, config.gr_host, config.gr_port,
          config.gr_username, config.gr_password, config.base_path);

// Serve config.js for grafana
// We should use special config.js for the frontend and point the ES to __es/
app.get(config.base_path + '/config.js', grafanaconfigjs);

// Serve all grafana frontend files
app.use(express.compress());
app.use(config.base_path + '/', express.static(__dirname + '/grafana/src', {maxAge: config.brower_cache_maxage || 0}));


run();

function run() {
  if (config.enable_ssl_port === true) {
    var options = {
      key: fs.readFileSync(config.ssl_key_file),
      cert: fs.readFileSync(config.ssl_cert_file),
    };
{'fraction_non_alphanumeric': 0.1558344259224574, 'fraction_numerical': 0.002622213897733658, 'mean_word_length': 4.266272189349112, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5035021', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1690, 'n_words': 370}