"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _clientBundle = _interopRequireDefault(require("./middleware/clientBundle"));

var _compression = _interopRequireDefault(require("compression"));

var _CLIENT_CONFIG = _interopRequireDefault(require("CLIENT_CONFIG"));

var _SERVER_CONFIG = _interopRequireDefault(require("SERVER_CONFIG"));

var _client = _interopRequireDefault(require("../configFiles/client.config"));

var _server = _interopRequireDefault(require("../configFiles/server.config"));

var _errorHandlers = _interopRequireDefault(require("./middleware/errorHandlers"));

var _express = _interopRequireDefault(require("express"));

var _paths = require("@bluebase/cli-core/lib/utils/paths");

var _fs = _interopRequireDefault(require("fs"));

var _logger = _interopRequireDefault(require("@bluebase/cli-core/lib/utils/logger"));

var _reactApplication = _interopRequireDefault(require("./middleware/reactApplication"));

var _resolvePaths = require("../helpers/resolvePaths");

var _security = _interopRequireDefault(require("./middleware/security"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

///////////////////////////
///// Load paths.json /////
///////////////////////////
const configsPath = (0, _paths.fromProjectRoot)('./build/web/server/paths.json');

if (!_fs.default.existsSync(configsPath)) {
  throw new Error(`We could not find the "paths.json" file. Please ensure that the server bundle has been built.`);
}

const paths = JSON.parse(_fs.default.readFileSync(configsPath, 'utf8')); ////////////////////////////////
///// Build Configs Bundle /////
////////////////////////////////
// Client Configs

let clientConfigs = (0, _client.default)({}, paths);
clientConfigs = (0, _CLIENT_CONFIG.default)(clientConfigs, paths); // Client Configs

let serverConfigs = (0, _server.default)({}, paths);
serverConfigs = (0, _SERVER_CONFIG.default)(serverConfigs, paths); ///////////////////
///// Configs /////
///////////////////

const configs = _objectSpread({}, (0, _resolvePaths.resolvePaths)(paths), {
  clientConfigs,
  serverConfigs
}); //////////////////
///// Server /////
//////////////////
// Create our express based server.


const app = (0, _express.default)(); // Don't expose any software information to potential hackers.

app.disable('x-powered-by'); // Security middlewares.

app.use(...(0, _security.default)(configs)); // Gzip compress the responses.

app.use((0, _compression.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 = serverConfigs.serviceWorker;
// if (process.env.BUILD_FLAG_IS_DEV === 'false' && serviceWorkerConfigs.enabled) {
// 	app.get(`/${serviceWorkerConfigs.fileName}`, serviceWorker(configs));
// 	app.get(
// 		`${clientConfigs.publicPath}${serviceWorkerConfigs.offlinePageFileName}`,
// 		offlinePage(configs),
// 	);
// }
// Configure serving of our client bundle.

if (clientConfigs.publicPath) {
  app.use(clientConfigs.publicPath, (0, _clientBundle.default)(configs));
} // Configure static serving of our "public" root http path static files.


app.use(_express.default.static(configs.assetsDir)); // The React application middleware.

app.get('*', (request, response) => {
  _logger.default.log({
    label: '@bluebase/cli/web',
    level: 'info',
    message: `Received for "${request.url}"`
  });

  return (0, _reactApplication.default)(configs)(request, response);
}); // Error Handler middlewares.

app.use(..._errorHandlers.default); // Create an http listener for our express app.

app.listen(serverConfigs.port, () => {
  _logger.default.log({
    label: '@bluebase/cli/web',
    level: 'info',
    message: `✓

		${serverConfigs.welcomeMessage}

		${clientConfigs.htmlPage.defaultTitle} is ready!

		with

		Service Workers: ${serverConfigs.serviceWorker.enabled}
		Server Side Rendering: ${!serverConfigs.disableSSR}

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



	`
  });
});