"use strict";

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

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

var _indexModuleD = _interopRequireDefault(require("../../index.module.d.js"));

var _fs = require("fs");

var _Options = _interopRequireDefault(require("./../../Options"));

var _chalk = _interopRequireDefault(require("chalk"));

/** Melakukan Setting Export Default Untuk File JS Ini **/
var _default = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(config) {
    var AppEngine, mSetting, _mSetting, appCookie;

    return _regenerator["default"].wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            /**
             * Melakukan Pengecekan Apakah Server Mode DEVELOPMENT ATAU PRODUCTION
             * Jika True, Maka Server Menampilkan Log System, Jika Tidak Mematikan Mode Logger
             *
             */
            if (config.serverState === _Options["default"].SERVER_STATE_DEVELOPMENT) {
              /** Memuat Fastify Module Dengan logger True **/
              mSetting = {
                logger: {
                  level: 'warn'
                },
                connectionTimeout: 180000,
                trustProxy: true
              };

              if (config.http2) {
                mSetting.http2 = true;
              }

              if (config.secure !== false) {
                mSetting.https = config.secure;
              }

              AppEngine = require("fastify")(mSetting);
              /** Melakukan Resolve Chain Promise **/
            } else if (config.serverState === _Options["default"].SERVER_STATE_PRODUCTION) {
              /** Memuat Fastify Module Dengan logger True **/
              _mSetting = {
                logger: {
                  level: 'warn'
                },
                connectionTimeout: 180000,
                trustProxy: true
              };

              if (config.http2) {
                _mSetting.http2 = true;
              }

              if (config.secure !== false) {
                _mSetting.https = config.secure;
              }

              AppEngine = require("fastify")(_mSetting);
              /** Melakukan Resolve Chain Promise **/
            }
            /**
             * Akhir Dari Melakukan Pengecekan Apakah Server Mode DEVELOPMENT ATAU PRODUCTION
             * Jika True, Maka Server Menampilkan Log System, Jika Tidak Mematikan Mode Logger
             *
             */

            /** Menambahkan Dekorasi Ke App Engin e Socket IO **/


            _context3.next = 3;
            return AppEngine.register(require('fastify-socket.io'), {
              cors: {
                origin: '*'
              }
            });

          case 3:
            _context3.t0 = config.serverView;
            _context3.next = 6;
            break;

          case 6:
            if (!(0, _fs.existsSync)(config.options.layoutDir)) {
              _context3.next = 11;
              break;
            }

            _context3.next = 9;
            return AppEngine.register(require('point-of-view'), {
              engine: {
                ejs: require('ejs')
              },
              root: config.options.layoutDir,

              /*viewExt: 'html'*/
              includeViewExtension: true
            });

          case 9:
            _context3.next = 12;
            break;

          case 11:
            console.info(_chalk["default"].red(" Pengaturan \"options.layoutDir\" Tidak Ditemukan. Harap Mendeklarasikan \"options.layoutDir\" Di Dalam Project \"".concat(config.serverName, "\" Atau Membuat Folder \"Layout\" di Folder Project ")));

          case 12:
            _context3.next = 14;
            return AppEngine.register(require("fastify-cors"));

          case 14:
            _context3.next = 16;
            return AppEngine.register(require("fastify-rate-limit"), {
              global: true,
              max: 1000,
              timeWindow: '1 minute'
            });

          case 16:
            _context3.next = 18;
            return AppEngine.register(require('fastify-multipart'), {
              attachFieldsToBody: true
            });

          case 18:
            appCookie = require('fastify-cookie');
            _context3.next = 21;
            return AppEngine.register(appCookie, {
              secret: config.settings.secretKey,
              path: "/"
            });

          case 21:
            _context3.next = 23;
            return AppEngine.register(require('fastify-helmet'), {
              contentSecurityPolicy: false
            });

          case 23:
            _context3.next = 25;
            return AppEngine.register(require('fastify-log'));

          case 25:
            _context3.next = 27;
            return AppEngine.register(require('fastify-graceful-shutdown'));

          case 27:
            _context3.next = 29;
            return AppEngine.register(require("fastify-jwt"), {
              secret: _indexModuleD["default"].config.SecretConfig.EncryptSecret
            });

          case 29:
            _context3.next = 31;
            return AppEngine.register(require("fastify-formbody"));

          case 31:
            _context3.next = 33;
            return AppEngine.after( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
              return _regenerator["default"].wrap(function _callee2$(_context2) {
                while (1) {
                  switch (_context2.prev = _context2.next) {
                    case 0:
                      _context2.next = 2;
                      return AppEngine.gracefulShutdown( /*#__PURE__*/function () {
                        var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(signal, next) {
                          return _regenerator["default"].wrap(function _callee$(_context) {
                            while (1) {
                              switch (_context.prev = _context.next) {
                                case 0:
                                  console.log("graceful ".concat(signal));
                                  next();

                                case 2:
                                case "end":
                                  return _context.stop();
                              }
                            }
                          }, _callee);
                        }));

                        return function (_x2, _x3) {
                          return _ref3.apply(this, arguments);
                        };
                      }());

                    case 2:
                    case "end":
                      return _context2.stop();
                  }
                }
              }, _callee2);
            })));

          case 33:
            if (!(0, _fs.existsSync)(config.options.assetsDir)) {
              _context3.next = 38;
              break;
            }

            _context3.next = 36;
            return AppEngine.register(require("fastify-static"), {
              root: config.options.assetsDir,
              decorateReply: false
            });

          case 36:
            _context3.next = 39;
            break;

          case 38:
            console.info(_chalk["default"].blue("Folder \"assetsDir\" Tidak Ditemukan. Harap Mendeklarasikan \"options.assetsDir\" Di Dalam Project \"".concat(config.serverName, "\" Atau Membuat Folder \"Assets\" di Folder Project")));

          case 39:
            if (!(0, _fs.existsSync)(config.options.uploadDir)) {
              _context3.next = 44;
              break;
            }

            _context3.next = 42;
            return AppEngine.register(require("fastify-static"), {
              root: config.options.uploadDir,
              prefix: '/upload/',
              decorateReply: false
            });

          case 42:
            _context3.next = 45;
            break;

          case 44:
            console.info(_chalk["default"].blue(" Folder \"uploadDir\" Tidak Ditemukan. Harap Mendeklarasikan \"options.uploadDir\" Di Dalam Project \"".concat(config.serverName, "\" Atau Membuat Folder \"Upload\" di Folder Project")));

          case 45:
            return _context3.abrupt("return", AppEngine);

          case 46:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));

  return function (_x) {
    return _ref.apply(this, arguments);
  };
}();

exports["default"] = _default;
//# sourceMappingURL=index.js.map