"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Logger_1 = require("./Logger");
Logger_1.Logger.configure(false, false);
const WebServer_1 = require("./WebServer");
const WebSocketServer_1 = require("./WebSocketServer");
const DatabaseConnection_1 = require("./DatabaseConnection");
const Crypt_1 = require("./Crypt");
const Config_1 = require("./Config");
const amqpwrapper_1 = require("./amqpwrapper");
const openflow_api_1 = require("@openiap/openflow-api");
const OAuthProvider_1 = require("./OAuthProvider");
const QueueClient_1 = require("./QueueClient");
const Message_1 = require("./Messages/Message");
Config_1.Config.db = new DatabaseConnection_1.DatabaseConnection(Config_1.Config.mongodb_url, Config_1.Config.mongodb_db, true);
let amqp = null;
async function initamqp(parent) {
    const span = Logger_1.Logger.otel.startSubSpan("initamqp", parent);
    try {
        amqp = new amqpwrapper_1.amqpwrapper(Config_1.Config.amqp_url);
        amqpwrapper_1.amqpwrapper.SetInstance(amqp);
        await amqp.connect(span);
    }
    catch (error) {
        span === null || span === void 0 ? void 0 : span.recordException(error);
        Logger_1.Logger.instanse.error("index", "initamqp", error);
        return false;
    }
    finally {
        Logger_1.Logger.otel.endSpan(span);
    }
}
async function ValidateUserForm(parent) {
    const span = Logger_1.Logger.otel.startSubSpan("ValidateUserForm", parent);
    try {
        var forms = await Config_1.Config.db.query({ query: { _id: Config_1.Config.validate_user_form, _type: "form" }, top: 1, collectionname: "forms", jwt: Crypt_1.Crypt.rootToken() }, null);
        if (forms.length == 0) {
            Logger_1.Logger.instanse.info("index", "ValidateUserForm", "validate_user_form " + Config_1.Config.validate_user_form + " does not exists!");
            Config_1.Config.validate_user_form = "";
        }
    }
    catch (error) {
        span === null || span === void 0 ? void 0 : span.recordException(error);
        Logger_1.Logger.instanse.error("index", "ValidateUserForm", error);
        return false;
    }
    finally {
        Logger_1.Logger.otel.endSpan(span);
    }
}
function doHouseKeeping() {
    // Message.lastHouseKeeping = new Date();
    if (Message_1.Message.lastHouseKeeping == null) {
        Message_1.Message.lastHouseKeeping = new Date();
        Message_1.Message.lastHouseKeeping.setDate(Message_1.Message.lastHouseKeeping.getDate() - 1);
    }
    amqpwrapper_1.amqpwrapper.Instance().send("openflow", "", { "command": "housekeeping", "lastrun": (new Date()).toISOString() }, 20000, null, "", 1);
    var dt = new Date(Message_1.Message.lastHouseKeeping.toISOString());
    var msg2 = new Message_1.Message();
    msg2.jwt = Crypt_1.Crypt.rootToken();
    var h = dt.getHours();
    var skipUpdateUsage = !(dt.getHours() == 1 || dt.getHours() == 13);
    if (Config_1.Config.NODE_ENV == "production") {
        msg2._Housekeeping(false, skipUpdateUsage, skipUpdateUsage, null).catch((error) => Logger_1.Logger.instanse.error("index", "doHouseKeeping", error));
    }
    else {
        // While debugging, always do all calculations
        msg2._Housekeeping(false, false, false, null).catch((error) => Logger_1.Logger.instanse.error("index", "doHouseKeeping", error));
        // msg2._Housekeeping(true, true, true, null).catch((error) => Logger.instanse.error("index", "doHouseKeeping", error));
    }
}
async function initDatabase(parent) {
    const span = Logger_1.Logger.otel.startSubSpan("initDatabase", parent);
    try {
        Logger_1.Logger.instanse.info("index", "initDatabase", "Begin validating builtin roles");
        const jwt = Crypt_1.Crypt.rootToken();
        const admins = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "admins", openflow_api_1.WellknownIds.admins, span);
        const users = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "users", openflow_api_1.WellknownIds.users, span);
        const root = await Logger_1.Logger.DBHelper.EnsureUser(jwt, "root", "root", openflow_api_1.WellknownIds.root, null, null, span);
        openflow_api_1.Base.addRight(root, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(root, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        openflow_api_1.Base.addRight(root, openflow_api_1.WellknownIds.root, "root", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(root, openflow_api_1.WellknownIds.root, [openflow_api_1.Rights.delete]);
        await Logger_1.Logger.DBHelper.Save(root, jwt, span);
        const robot_agent_users = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "robot agent users", openflow_api_1.WellknownIds.robot_agent_users, span);
        openflow_api_1.Base.addRight(robot_agent_users, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(robot_agent_users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        openflow_api_1.Base.addRight(robot_agent_users, openflow_api_1.WellknownIds.root, "root", [openflow_api_1.Rights.full_control]);
        if (Config_1.Config.multi_tenant) {
            Logger_1.Logger.instanse.silly("index", "initDatabase", "[root][users] Running in multi tenant mode, remove " + robot_agent_users.name + " from self");
            openflow_api_1.Base.removeRight(robot_agent_users, robot_agent_users._id, [openflow_api_1.Rights.full_control]);
        }
        else if (Config_1.Config.update_acl_based_on_groups) {
            openflow_api_1.Base.removeRight(robot_agent_users, robot_agent_users._id, [openflow_api_1.Rights.full_control]);
            openflow_api_1.Base.addRight(robot_agent_users, robot_agent_users._id, "robot agent users", [openflow_api_1.Rights.read]);
        }
        await Logger_1.Logger.DBHelper.Save(robot_agent_users, jwt, span);
        openflow_api_1.Base.addRight(admins, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(admins, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        await Logger_1.Logger.DBHelper.Save(admins, jwt, span);
        openflow_api_1.Base.addRight(users, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        users.AddMember(root);
        if (Config_1.Config.multi_tenant) {
            openflow_api_1.Base.removeRight(users, users._id, [openflow_api_1.Rights.full_control]);
        }
        else {
            openflow_api_1.Base.removeRight(users, users._id, [openflow_api_1.Rights.full_control]);
            openflow_api_1.Base.addRight(users, users._id, "users", [openflow_api_1.Rights.read]);
        }
        await Logger_1.Logger.DBHelper.Save(users, jwt, span);
        const personal_nodered_users = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "personal nodered users", openflow_api_1.WellknownIds.personal_nodered_users, span);
        personal_nodered_users.AddMember(admins);
        openflow_api_1.Base.addRight(personal_nodered_users, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(personal_nodered_users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        if (Config_1.Config.multi_tenant) {
            Logger_1.Logger.instanse.silly("index", "initDatabase", "[root][users] Running in multi tenant mode, remove " + personal_nodered_users.name + " from self");
            openflow_api_1.Base.removeRight(personal_nodered_users, personal_nodered_users._id, [openflow_api_1.Rights.full_control]);
        }
        else if (Config_1.Config.update_acl_based_on_groups) {
            openflow_api_1.Base.removeRight(personal_nodered_users, personal_nodered_users._id, [openflow_api_1.Rights.full_control]);
            openflow_api_1.Base.addRight(personal_nodered_users, personal_nodered_users._id, "personal nodered users", [openflow_api_1.Rights.read]);
        }
        await Logger_1.Logger.DBHelper.Save(personal_nodered_users, jwt, span);
        const nodered_admins = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "nodered admins", openflow_api_1.WellknownIds.nodered_admins, span);
        nodered_admins.AddMember(admins);
        openflow_api_1.Base.addRight(nodered_admins, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(nodered_admins, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        await Logger_1.Logger.DBHelper.Save(nodered_admins, jwt, span);
        const nodered_users = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "nodered users", openflow_api_1.WellknownIds.nodered_users, span);
        nodered_users.AddMember(admins);
        openflow_api_1.Base.addRight(nodered_users, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(nodered_users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        if (Config_1.Config.multi_tenant) {
            Logger_1.Logger.instanse.silly("index", "initDatabase", "[root][users] Running in multi tenant mode, remove " + nodered_users.name + " from self");
            openflow_api_1.Base.removeRight(nodered_users, nodered_users._id, [openflow_api_1.Rights.full_control]);
        }
        else if (Config_1.Config.update_acl_based_on_groups) {
            openflow_api_1.Base.removeRight(nodered_users, nodered_users._id, [openflow_api_1.Rights.full_control]);
            openflow_api_1.Base.addRight(nodered_users, nodered_users._id, "nodered users", [openflow_api_1.Rights.read]);
        }
        await Logger_1.Logger.DBHelper.Save(nodered_users, jwt, span);
        const nodered_api_users = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "nodered api users", openflow_api_1.WellknownIds.nodered_api_users, span);
        nodered_api_users.AddMember(admins);
        openflow_api_1.Base.addRight(nodered_api_users, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(nodered_api_users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        if (Config_1.Config.multi_tenant) {
            Logger_1.Logger.instanse.silly("index", "initDatabase", "[root][users] Running in multi tenant mode, remove " + nodered_api_users.name + " from self");
            openflow_api_1.Base.removeRight(nodered_api_users, nodered_api_users._id, [openflow_api_1.Rights.full_control]);
        }
        else if (Config_1.Config.update_acl_based_on_groups) {
            openflow_api_1.Base.removeRight(nodered_api_users, nodered_api_users._id, [openflow_api_1.Rights.full_control]);
            openflow_api_1.Base.addRight(nodered_api_users, nodered_api_users._id, "nodered api users", [openflow_api_1.Rights.read]);
        }
        await Logger_1.Logger.DBHelper.Save(nodered_api_users, jwt, span);
        if (Config_1.Config.multi_tenant) {
            try {
                const resellers = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "resellers", openflow_api_1.WellknownIds.resellers, span);
                // @ts-ignore
                resellers.hidemembers = true;
                openflow_api_1.Base.addRight(resellers, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
                openflow_api_1.Base.removeRight(resellers, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
                openflow_api_1.Base.removeRight(resellers, openflow_api_1.WellknownIds.resellers, [openflow_api_1.Rights.full_control]);
                resellers.AddMember(admins);
                await Logger_1.Logger.DBHelper.Save(resellers, jwt, span);
                const customer_admins = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "customer admins", openflow_api_1.WellknownIds.customer_admins, span);
                // @ts-ignore
                customer_admins.hidemembers = true;
                openflow_api_1.Base.addRight(customer_admins, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
                openflow_api_1.Base.removeRight(customer_admins, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
                openflow_api_1.Base.removeRight(customer_admins, openflow_api_1.WellknownIds.customer_admins, [openflow_api_1.Rights.full_control]);
                await Logger_1.Logger.DBHelper.Save(customer_admins, jwt, span);
            }
            catch (error) {
                Logger_1.Logger.instanse.error("index", "initDatabase", error);
            }
        }
        const robot_admins = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "robot admins", openflow_api_1.WellknownIds.robot_admins, span);
        robot_admins.AddMember(admins);
        openflow_api_1.Base.addRight(robot_admins, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(robot_admins, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        await Logger_1.Logger.DBHelper.Save(robot_admins, jwt, span);
        const robot_users = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "robot users", openflow_api_1.WellknownIds.robot_users, span);
        robot_users.AddMember(admins);
        robot_users.AddMember(users);
        openflow_api_1.Base.addRight(robot_users, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(robot_users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        if (Config_1.Config.multi_tenant) {
            Logger_1.Logger.instanse.silly("index", "initDatabase", "[root][users] Running in multi tenant mode, remove " + robot_users.name + " from self");
            openflow_api_1.Base.removeRight(robot_users, robot_users._id, [openflow_api_1.Rights.full_control]);
        }
        else if (Config_1.Config.update_acl_based_on_groups) {
            openflow_api_1.Base.removeRight(robot_users, robot_users._id, [openflow_api_1.Rights.full_control]);
            openflow_api_1.Base.addRight(robot_users, robot_users._id, "robot users", [openflow_api_1.Rights.read, openflow_api_1.Rights.invoke, openflow_api_1.Rights.update]);
        }
        await Logger_1.Logger.DBHelper.Save(robot_users, jwt, span);
        if (!admins.IsMember(root._id)) {
            admins.AddMember(root);
            await Logger_1.Logger.DBHelper.Save(admins, jwt, span);
        }
        const filestore_admins = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "filestore admins", openflow_api_1.WellknownIds.filestore_admins, span);
        filestore_admins.AddMember(admins);
        openflow_api_1.Base.addRight(filestore_admins, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(filestore_admins, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        if (Config_1.Config.multi_tenant) {
            Logger_1.Logger.instanse.silly("index", "initDatabase", "[root][users] Running in multi tenant mode, remove " + filestore_admins.name + " from self");
            openflow_api_1.Base.removeRight(filestore_admins, filestore_admins._id, [openflow_api_1.Rights.full_control]);
        }
        await Logger_1.Logger.DBHelper.Save(filestore_admins, jwt, span);
        const filestore_users = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "filestore users", openflow_api_1.WellknownIds.filestore_users, span);
        filestore_users.AddMember(admins);
        if (!Config_1.Config.multi_tenant) {
            filestore_users.AddMember(users);
        }
        openflow_api_1.Base.addRight(filestore_users, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(filestore_users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        if (Config_1.Config.multi_tenant) {
            Logger_1.Logger.instanse.silly("index", "initDatabase", "[root][users] Running in multi tenant mode, remove " + filestore_users.name + " from self");
            openflow_api_1.Base.removeRight(filestore_users, filestore_users._id, [openflow_api_1.Rights.full_control]);
        }
        else if (Config_1.Config.update_acl_based_on_groups) {
            openflow_api_1.Base.removeRight(filestore_users, filestore_users._id, [openflow_api_1.Rights.full_control]);
            openflow_api_1.Base.addRight(filestore_users, filestore_users._id, "filestore users", [openflow_api_1.Rights.read]);
        }
        await Logger_1.Logger.DBHelper.Save(filestore_users, jwt, span);
        const workitem_queue_admins = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "workitem queue admins", "625440c4231309af5f2052cd", span);
        workitem_queue_admins.AddMember(admins);
        openflow_api_1.Base.addRight(workitem_queue_admins, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(workitem_queue_admins, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        if (Config_1.Config.multi_tenant) {
            openflow_api_1.Base.removeRight(workitem_queue_admins, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.full_control]);
        }
        await Logger_1.Logger.DBHelper.Save(workitem_queue_admins, jwt, span);
        const workitem_queue_users = await Logger_1.Logger.DBHelper.EnsureRole(jwt, "workitem queue users", "62544134231309e2cd2052ce", span);
        openflow_api_1.Base.addRight(workitem_queue_users, openflow_api_1.WellknownIds.admins, "admins", [openflow_api_1.Rights.full_control]);
        openflow_api_1.Base.removeRight(workitem_queue_users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.delete]);
        if (Config_1.Config.multi_tenant) {
            openflow_api_1.Base.removeRight(workitem_queue_users, openflow_api_1.WellknownIds.admins, [openflow_api_1.Rights.full_control]);
        }
        await Logger_1.Logger.DBHelper.Save(workitem_queue_users, jwt, span);
        await Config_1.Config.db.ensureindexes(span);
        if (Config_1.Config.auto_hourly_housekeeping) {
            const crypto = require('crypto');
            const randomNum = crypto.randomInt(1, 100);
            // Every 15 minutes, give and take a few minutes, send out a message to do house keeping, if ready
            Logger_1.Logger.instanse.verbose("index", "initDatabase", "Housekeeping every 15 minutes plus " + randomNum + " seconds");
            housekeeping = setInterval(async () => {
                if (Config_1.Config.enable_openflow_amqp) {
                    if (!Message_1.Message.ReadyForHousekeeping()) {
                        return;
                    }
                    amqpwrapper_1.amqpwrapper.Instance().send("openflow", "", { "command": "housekeeping" }, 10000, null, "", 1);
                    await new Promise(resolve => { setTimeout(resolve, 10000); });
                    if (Message_1.Message.ReadyForHousekeeping()) {
                        doHouseKeeping();
                    }
                    else {
                        Logger_1.Logger.instanse.verbose("index", "initDatabase", "SKIP housekeeping");
                    }
                }
                else {
                    doHouseKeeping();
                }
            }, (15 * 60 * 1000) + (randomNum * 1000));
            // If I'm first and noone else has run it, lets trigger it now
            const randomNum2 = crypto.randomInt(1, 10);
            Logger_1.Logger.instanse.info("index", "initDatabase", "Trigger first Housekeeping in " + randomNum2 + " seconds");
            setTimeout(async () => {
                if (Config_1.Config.enable_openflow_amqp) {
                    if (!Message_1.Message.ReadyForHousekeeping()) {
                        return;
                    }
                    amqpwrapper_1.amqpwrapper.Instance().send("openflow", "", { "command": "housekeeping" }, 10000, null, "", 1);
                    await new Promise(resolve => { setTimeout(resolve, 10000); });
                    if (Message_1.Message.ReadyForHousekeeping()) {
                        doHouseKeeping();
                    }
                    else {
                        Logger_1.Logger.instanse.verbose("index", "initDatabase", "SKIP housekeeping");
                    }
                }
                else {
                    doHouseKeeping();
                }
            }, randomNum2 * 1000);
        }
        return true;
    }
    catch (error) {
        span === null || span === void 0 ? void 0 : span.recordException(error);
        Logger_1.Logger.instanse.error("index", "initDatabase", error);
        return false;
    }
    finally {
        Logger_1.Logger.otel.endSpan(span);
    }
}
process.on('beforeExit', (code) => {
    Logger_1.Logger.instanse.error("index", "beforeExit", code);
});
process.on('exit', (code) => {
    Logger_1.Logger.instanse.error("index", "exit", code);
});
const unhandledRejections = new Map();
process.on('unhandledRejection', (reason, promise) => {
    Logger_1.Logger.instanse.error("index", "unhandledRejection", reason);
    // ('Unhandled Rejection at: Promise', promise, 'reason:', reason);
    unhandledRejections.set(promise, reason);
});
process.on('rejectionHandled', (promise) => {
    unhandledRejections.delete(promise);
});
process.on('uncaughtException', (err, origin) => {
    Logger_1.Logger.instanse.error("index", "uncaughtException", err);
    // (`Caught exception: ${err}\n` +
    //     `Exception origin: ${origin}`
    // );
});
process.on('uncaughtExceptionMonitor', (err, origin) => {
    Logger_1.Logger.instanse.error("index", "uncaughtExceptionMonitor", err);
    // (`Caught exception Monitor: ${err}\n` +
    //     `Exception origin: ${origin}`
    // );
});
process.on('warning', (warning) => {
    try {
        Logger_1.Logger.instanse.warn("index", "uncaughtExceptionMonitor", warning.name + ": " + warning.message);
        // (warning.name + ": " + warning.message);
        // (warning.stack);
    }
    catch (error) {
    }
});
// The signals we want to handle
// NOTE: although it is tempting, the SIGKILL signal (9) cannot be intercepted and handled
var signals = {
    'SIGHUP': 1,
    'SIGINT': 2,
    'SIGTERM': 15
};
var housekeeping = null;
function handle(signal, value) {
    Logger_1.Logger.instanse.info("index", "handle", `process received a ${signal} signal with value ${value}`);
    try {
        Config_1.Config.db.shutdown();
        Logger_1.Logger.otel.shutdown();
        Logger_1.Logger.License.shutdown();
        // Auth.shutdown();
        if (housekeeping != null) {
            try {
                clearInterval(housekeeping);
            }
            catch (error) {
            }
        }
        setTimeout(() => {
            process.exit(128 + value);
        }, 1000);
        server.close((err) => {
            Logger_1.Logger.instanse.info("index", "handle", `server stopped by ${signal} with value ${value}`);
            Logger_1.Logger.instanse.error("index", "handle", err);
            process.exit(128 + value);
        });
    }
    catch (error) {
        Logger_1.Logger.instanse.error("index", "handle", error);
        Logger_1.Logger.instanse.info("index", "handle", `server stopped by ${signal} with value ${value}`);
        process.exit(128 + value);
    }
}
Object.keys(signals).forEach((signal) => process.on(signal, handle));
let GrafanaProxy = null;
try {
    GrafanaProxy = require("./ee/grafana-proxy");
}
catch (error) {
}
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
const originalStderrWrite = process.stderr.write.bind(process.stderr);
process.stdout.write = (chunk, encoding, callback) => {
    return originalStdoutWrite(chunk, encoding, callback);
};
process.stderr.write = (chunk, encoding, callback) => {
    return originalStderrWrite(chunk, encoding, callback);
};
var server = null;
(async function () {
    const span = Logger_1.Logger.otel.startSpan("openflow.startup");
    try {
        await initamqp(span);
        Logger_1.Logger.instanse.info("index", "configure", "VERSION: " + Config_1.Config.version);
        if (Logger_1.Logger.License.validlicense) {
            if (openflow_api_1.NoderedUtil.IsNullEmpty(Logger_1.Logger.License.data.domain)) {
                Logger_1.Logger.instanse.info("index", "configure", "License valid to " + Logger_1.Logger.License.data.expirationDate);
            }
            else {
                Logger_1.Logger.instanse.info("index", "configure", "License valid for " + Logger_1.Logger.License.data.domain + " until the " + Logger_1.Logger.License.data.expirationDate);
            }
        }
        server = await WebServer_1.WebServer.configure(Config_1.Config.baseurl(), span);
        if (GrafanaProxy != null) {
            const grafana = await GrafanaProxy.GrafanaProxy.configure(WebServer_1.WebServer.app, span);
        }
        OAuthProvider_1.OAuthProvider.configure(WebServer_1.WebServer.app, span);
        WebSocketServer_1.WebSocketServer.configure(server, span);
        await QueueClient_1.QueueClient.configure(span);
        await ValidateUserForm(span);
        if (!await initDatabase(span)) {
            process.exit(404);
        }
        WebServer_1.WebServer.Listen();
        Config_1.Config.db.queuemonitoring();
    }
    catch (error) {
        span === null || span === void 0 ? void 0 : span.recordException(error);
        Logger_1.Logger.instanse.error("index", "configure", error);
        process.exit(404);
    }
    finally {
        Logger_1.Logger.otel.endSpan(span);
    }
})();
//# sourceMappingURL=index.js.map