'use strict';

// global requires
global.logger = require('./utils/Logger').loggers.get('main');

var utilBanner = require('./utils/UtilBanner');

// print the banner to show the app is starting. This should be as early in the startup as possible, after the logging is set up
var pjson = require('../package.json');
utilBanner.printStartingBanner(pjson);

global.config = require('./configLoader/Config.js');
global.SharedConstants = require('../shared/constants/SharedConstants');

config.load().then(function successFunc() {
        // setup event loop blocking detection if configured
        var EventLoopMonitor = require('./monitoring/EventLoopMonitor');
        EventLoopMonitor.startEventLoopDetection();

        logger.info("Starting express web server.");

        const UtilElasticSearch = require('./utils/UtilElasticSearch');
        if (UtilElasticSearch.disableIndexing()) {
            logger.warn("ElasticSearch indexing is disabled.");
        }

        var server = require('./web/ExpressServer.js');
        server.start();
    },
    function errorFunc(err) {
        logger.error("Fatal error loading configuration - " + err);
        if(err && err.message.startsWith(SharedConstants.ERROR_DB_CONNECTION)){
          utilBanner.printCompletionBanner("Application shutdown - invalid database connection");
        }
        process.exit();
    }
);

