"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = run;
const util_1 = require("util");
const utils_1 = require("../utils");
const admin_server_utils_1 = require("./admin-server-utils");
async function run(args) {
    const options = {
        help: {
            type: 'boolean',
            short: 'h',
        },
        port: {
            type: 'string',
            short: 'p',
            description: 'Specified port to start the server on (default: 9090)',
        },
        root: {
            type: 'string',
            description: 'Path to the root of the JB2 installation',
        },
        bodySizeLimit: {
            type: 'string',
            description: 'Size limit of the update message (default: 25mb)',
        },
    };
    const { values: flags } = (0, util_1.parseArgs)({
        args,
        options,
        allowPositionals: true,
    });
    const description = 'Start up a small admin server for JBrowse configuration';
    const examples = ['$ jbrowse admin-server', '$ jbrowse admin-server -p 8888'];
    if (flags.help) {
        (0, utils_1.printHelp)({
            description,
            examples,
            usage: 'jbrowse admin-server [options]',
            options,
        });
        return;
    }
    const { root, bodySizeLimit = '25mb' } = flags;
    const { outFile, baseDir } = await (0, admin_server_utils_1.setupConfigFile)({ root });
    // Parse and validate port
    const port = (0, admin_server_utils_1.parsePort)({ portStr: flags.port });
    // Set up the Express server
    const { app, key, keyPath, serverRef } = (0, admin_server_utils_1.setupServer)({
        baseDir,
        outFile,
        bodySizeLimit,
    });
    // Start the server and set up shutdown handlers
    (0, admin_server_utils_1.startServer)({ app, port, key, outFile, keyPath, serverRef });
}
