"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.runConsoleAPI = void 0;

var _path = _interopRequireDefault(require("path"));

var _utils = require("@rivalis/utils");

var _fastify = _interopRequireDefault(require("fastify"));

var _fastifyCors = _interopRequireDefault(require("fastify-cors"));

var _fastifyStatic = _interopRequireDefault(require("fastify-static"));

var _authRouter = _interopRequireDefault(require("./routers/authRouter"));

var _infraRouter = _interopRequireDefault(require("./routers/infraRouter"));

var _database = require("./database");

var _appRouter = _interopRequireDefault(require("./routers/appRouter"));

var _FleetService = _interopRequireDefault(require("./services/FleetService"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const runConsoleAPI = () => {
  const PORT = (0, _utils.env)('CONSOLE_API_PORT', 2334, 'number');
  const ENABLE_WEBAPP = (0, _utils.env)('CONSOLE_ENABLE_WEBAPP', false, 'boolean');
  const httpServer = (0, _fastify.default)();

  const logger = _utils.Logger.getLogger('console/api');

  _utils.Logger.setLevel(_utils.Logger.LEVEL.DEBUG);

  logger.info('initializing...');
  httpServer.register(_fastifyCors.default);

  if (ENABLE_WEBAPP) {
    let staticPath = _path.default.join(__dirname, '../client');

    httpServer.register(_fastifyStatic.default, {
      root: staticPath
    });
  }

  httpServer.register(_authRouter.default, {
    prefix: '/api/auth'
  });
  httpServer.register(_infraRouter.default, {
    prefix: '/api/infrastructure'
  });
  httpServer.register(_appRouter.default, {
    prefix: '/api/apps'
  });
  (0, _database.databaseInit)().then(() => {
    _FleetService.default.getInstance().invokeUpdate();
  });
  httpServer.listen(PORT, (error = null) => {
    if (error !== null) {
      logger.error('server fail:', error.stack);
      process.exit();
    }

    logger.info(`server started port=(${PORT})`);
  });
};

exports.runConsoleAPI = runConsoleAPI;