"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cli_core_1 = require("@bluebase/cli-core");
const clientBundle_1 = __importDefault(require("./middleware/clientBundle"));
const compression_1 = __importDefault(require("compression"));
const errorHandlers_1 = __importDefault(require("./middleware/errorHandlers"));
const express_1 = __importDefault(require("express"));
const offlinePage_1 = __importDefault(require("./middleware/offlinePage"));
const reactApplication_1 = __importDefault(require("./middleware/reactApplication"));
const security_1 = __importDefault(require("./middleware/security"));
const serviceWorker_1 = __importDefault(require("./middleware/serviceWorker"));
const logger = cli_core_1.Utils.logger;
const server = (configs) => {
    // const configs: ServerConfigsBundle = {
    // 	assetsDirPath: JSON.parse(process.env.ASSETS_DIR_PATH as string),
    // 	client: JSON.parse(process.env.CLIENT_CONFIGS as string),
    // 	server: JSON.parse(process.env.SERVER_CONFIGS as string),
    // };
    // Create our express based server.
    const app = express_1.default();
    // Don't expose any software information to potential hackers.
    app.disable('x-powered-by');
    // Security middlewares.
    app.use(...security_1.default(configs));
    // Gzip compress the responses.
    app.use(compression_1.default());
    // Register our service worker generated by our webpack config.
    // We do not want the service worker registered for development builds, and
    // additionally only want it registered if the config allows.
    const serviceWorkerConfigs = configs.server.serviceWorker;
    if (process.env.BUILD_FLAG_IS_DEV === 'false' && serviceWorkerConfigs.enabled) {
        app.get(`/${serviceWorkerConfigs.fileName}`, serviceWorker_1.default(configs));
        app.get(`${configs.client.publicPath}${serviceWorkerConfigs.offlinePageFileName}`, offlinePage_1.default(configs));
    }
    // Configure serving of our client bundle.
    if (configs.client.publicPath) {
        app.use(configs.client.publicPath, clientBundle_1.default(configs));
    }
    // Configure static serving of our "public" root http path static files.
    app.use(express_1.default.static(configs.assetsDirPath));
    // The React application middleware.
    app.get('*', (request, response) => {
        logger.log({
            label: '@bluebase/cli/server',
            level: 'info',
            message: `Received for "${request.url}"`,
        });
        return reactApplication_1.default(configs)(request, response);
    });
    // Error Handler middlewares.
    app.use(...errorHandlers_1.default);
    // Create an http listener for our express app.
    const listener = app.listen(configs.server.port, () => {
        logger.log({
            label: '@bluebase/cli/server',
            level: 'info',
            // tslint:disable-next-line:object-literal-sort-keys
            message: `✓

			${configs.server.welcomeMessage}

			${configs.client.htmlPage.defaultTitle} is ready!

			with

			Service Workers: ${serviceWorkerConfigs.enabled}
			Server Side Rendering: ${!configs.server.disableSSR}

			Server is now listening on Port ${configs.server.port}
			You can access it in the browser at http://${configs.server.host}:${configs.server.port}
			Press Ctrl-C to stop.



		`,
        });
    });
    // We export the listener as it will be handy for our development hot reloader,
    // or for exposing a general extension layer for application customisations.
    return listener;
};
exports.default = server;
//# sourceMappingURL=server.js.map