"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var compression_1 = __importDefault(require("compression"));
var connect_history_api_fallback_1 = __importDefault(require("connect-history-api-fallback"));
var express_1 = __importDefault(require("express"));
var fs_1 = __importDefault(require("fs"));
var https_1 = __importDefault(require("https"));
var morgan_1 = __importDefault(require("morgan"));
var path_1 = __importDefault(require("path"));
var serve_static_1 = __importDefault(require("serve-static"));
var app = express_1["default"]();
var publicDir = 'assets';
// const defaultPage = 'index.html';
var port = 8443;
var ss = serve_static_1["default"].mime;
function setCustomCacheControl(res, path) {
    if (ss.lookup(path) === 'text/html') {
        // index.html will cached 10 mins
        res.setHeader('Cache-Control', 'public, max-age=600');
    }
}
app
    .use(morgan_1["default"]('combined')) // logger
    .use(compression_1["default"]()) // gzip
    .use('/live', function (req, res) {
    // OpenShift livenessProbe
    res.send('<h1>Application is alive :)</h1>');
})
    .use(connect_history_api_fallback_1["default"]({
    rewrites: [
        {
            from: /^\/solution-engine\/(.*)/i,
            to: function (context) {
                return '/' + context.match[1];
            }
        }
    ]
}))
    .use(serve_static_1["default"](path_1["default"].join(__dirname, publicDir), {
    maxAge: '10d',
    setHeaders: setCustomCacheControl
}));
try {
    var cert = fs_1["default"].readFileSync('/etc/ssl/server.crt');
    var key = fs_1["default"].readFileSync('/etc/ssl/server.key');
    var options = {
        cert: cert,
        key: key
    };
    https_1["default"].createServer(options, app).listen(port, function () {
        console.log('Application started with https');
    });
}
catch (err) {
    console.error(err);
    app.listen(port, function () {
        console.log('Application started with http');
    });
}
