"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const nodekit_1 = require("@gravity-ui/nodekit");
const expresskit_1 = require("@gravity-ui/expresskit");
const configure_app_1 = require("./configure-app");
const yt_auth_1 = require("./middlewares/yt-auth");
const routes_1 = __importDefault(require("./routes"));
const oauth_1 = require("./middlewares/oauth");
const authorization_1 = require("./middlewares/authorization");
const authorization_2 = require("./utils/authorization");
const apply_app_env_to_config_1 = require("./utils/configs/apply-app-env-to-config");
const check_configuration_1 = require("./middlewares/check-configuration");
const nodekit = new nodekit_1.NodeKit({ configsPath: path_1.default.resolve(__dirname, './configs') });
(0, apply_app_env_to_config_1.applyAppEnvToConfig)(nodekit.config);
const { appName, appEnv, appInstallation, appDevMode } = nodekit.config;
nodekit.ctx.log('AppConfig details', {
    appName,
    appEnv,
    appInstallation,
    appDevMode,
});
const { allowPasswordAuth, ytOAuthSettings } = nodekit.config;
const authMiddlewares = [];
if (ytOAuthSettings) {
    authMiddlewares.push((0, authorization_2.authorizationResolver)((0, oauth_1.createOAuthAuthorizationResolver)()));
}
if (allowPasswordAuth) {
    authMiddlewares.push((0, authorization_2.authorizationResolver)((0, yt_auth_1.createYTAuthorizationResolver)()));
}
if (authMiddlewares.length) {
    nodekit.config.appBeforeAuthMiddleware = [
        ...(nodekit.config.appBeforeAuthMiddleware || []),
        ...authMiddlewares,
    ];
    nodekit.config.appAuthHandler = (0, authorization_1.createAuthMiddleware)();
}
nodekit.config.adjustAppConfig?.(nodekit);
const configurationErrors = (0, check_configuration_1.createConfigurationErrorsMidleware)(nodekit);
if (configurationErrors) {
    nodekit.config.appBeforeAuthMiddleware = [configurationErrors];
}
const app = new expresskit_1.ExpressKit(nodekit, routes_1.default);
(0, configure_app_1.configureApp)(app);
if (require.main === module) {
    app.run();
}
exports.default = app;
/**
 * In some cases requests to unkonwn hosts might be failed with `Error: getaddrinfo EAI_AGAIN`,
 * such requests do not fail immedeately they wait for timeout before falling, and during the timeout
 * all other requests to valid hosts from nodejs are falling too.
 * Steps to reproduce:
 * 1. Run UI without the fix
 *  `./run_local_cluster.sh --yt-version dev --docker-hostname $(hostname) --fqdn localhost --ui-version 1.33.0`
 * 2. Run api/clusters/versions:
 *  `curl http://$(hostname):8001/api/clusters/versions &`
 * 3. Do not wait while request from item 2 is finished and run:
 *  `curl http://$(hostname):8001/api/cluster-info/ui`
 *   (in some rare cases it might be successfully finished)
 
 * The solution is taken from https://stackoverflow.com/questions/40182121/whats-the-cause-of-the-error-getaddrinfo-eai-again
 */
/**
 * In specific cases it may bring unneccessary exceptions with ESERVFAIL
 * see https://github.com/szmarczak/cacheable-lookup/issues/68
 * see also https://nda.ya.ru/t/GzZa7MD97SrcUW
 
import http from 'http';
import https from 'https';
import CacheableLookup from 'cacheable-lookup';

const cacheable = new CacheableLookup();

cacheable.install(http.globalAgent);
cacheable.install(https.globalAgent);
*/
