"use strict";
/**
 *  Dullahan daemon
 *
 *  Copyright (C) 2010-2020 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var dotenv = require("dotenv-flow");
var express = require("express");
var promBundle = require("express-prom-bundle");
var bodyParser = require("body-parser");
var events_1 = require("events");
var EventMediator_1 = require("../src/system/EventMediator");
var HttpClient_1 = require("../src/system/network/HttpClient");
var ProgramFactory_1 = require("../src/dullahan/program/ProgramFactory");
var RaterFactory_1 = require("../src/dullahan/program/RaterFactory");
var StandardLogger_1 = require("../src/system/StandardLogger");
var AccessLogger_1 = require("../src/dullahan/middleware/AccessLogger");
var ConsoleFactory_1 = require("../src/system/ConsoleFactory");
var IndicationController_1 = require("../src/dullahan/controllers/IndicationController");
var SystemController_1 = require("../src/dullahan/controllers/SystemController");
dotenv.config();
var app = express();
var env = process.env.NODE_ENV;
var log_format = process.env.DULLAHAN_ACCESS_LOG_FORMAT;
var port = process.env.NODE_PORT;
var program_id = process.env.DULLAHAN_PROGRAM_ID;
var rater_path = process.env.DULLAHAN_RATER_PATH;
var service = 'dullahan';
var log_console = ConsoleFactory_1.createConsole(process.env.LOG_PATH_DEBUG);
if (!env) {
    throw new Error('Unable to determine env.');
}
else if (!log_format) {
    throw new Error('Unable to read log_format from env.');
}
else if (!port) {
    throw new Error('Unable to read port from env.');
}
else if (!program_id) {
    throw new Error('Unable to read program_id from env.');
    // This is set in the dullahan script, not the .env
}
else if (!rater_path) {
    throw new Error('Unable to read rater_path from env.');
}
var metricsMiddleware = promBundle({ includePath: true });
var ts_ctor = function () { return Math.floor(new Date().getTime() / 1000); };
var emitter = new events_1.EventEmitter();
var http = new HttpClient_1.HttpClient();
var logger = new StandardLogger_1.StandardLogger(log_console, ts_ctor, env, service);
var program_path = "program/" + program_id + "/Program";
/* eslint-disable @typescript-eslint/no-var-requires */
var program = require(program_path);
var getQuoteDataBucket = require('../src/bucket/QuoteDataBucket');
/* eslint-enable @typescript-eslint/no-var-requires */
var rater_src = rater_path + "/" + process.env.DULLAHAN_PROGRAM_ID + ".js";
var program_factory = new ProgramFactory_1.ProgramFactory(program, function () { return getQuoteDataBucket(); });
var rater_factory = new RaterFactory_1.RaterFactory(rater_src);
new EventMediator_1.EventMediator(logger, emitter);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(metricsMiddleware);
app.use(AccessLogger_1.routeAccessEmitter(emitter));
app.use(AccessLogger_1.accessLogger(log_format));
app.get('/healthcheck', SystemController_1.system.healthcheck);
app.post('/indication', IndicationController_1.indication.create(emitter)(http)(program_factory)(rater_factory));
// Start the Express server
var server = app.listen(port, function () {
    return logger.info("Dullahan started at http://localhost:" + port + ".");
});
process.on('SIGTERM', function () {
    // events stop working here, switch to regular logger
    logger.info('SIGTERM signal received');
    server.close(function () {
        logger.info('HTTP server closed');
        // We want to use process.exit here because it is the proper way to
        // shut down this service.
        /* eslint-disable no-process-exit */
        process.exit(1);
        /* eslint-enable no-process-exit */
    });
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZHVsbGFoYW4uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJkdWxsYWhhbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7R0FtQkc7O0FBRUgsb0NBQXNDO0FBQ3RDLGlDQUFtQztBQUNuQyxnREFBa0Q7QUFDbE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