text: "use strict";
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      return new (P || (P = Promise))(function (resolve, reject) {
          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
          function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  var __importDefault = (this && this.__importDefault) || function (mod) {
      return (mod && mod.__esModule) ? mod : { "default": mod };
  }
  var __importStar = (this && this.__importStar) || function (mod) {
      if (mod && mod.__esModule) return mod;
      var result = {};
      if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
      result["default"] = mod;
      return result;
  }
  Object.defineProperty(exports, "__esModule", { value: true });
  const express_1 = __importDefault(require("express"));
  const compression_1 = __importDefault(require("compression")); // compresses requests
  const express_session_1 = __importDefault(require("express-session"));
  const body_parser_1 = __importDefault(require("body-parser"));
  const lusca_1 = __importDefault(require("lusca"));
  const dotenv_1 = __importDefault(require("dotenv"));
  const connect_mongo_1 = __importDefault(require("connect-mongo"));
  const express_flash_1 = __importDefault(require("express-flash"));
  const path_1 = __importDefault(require("path"));
  const mongoose_1 = __importDefault(require("mongoose"));
  const passport_1 = __importDefault(require("passport"));
  const express_ejs_layouts_1 = __importDefault(require("express-ejs-layouts"));
  const express_validator_1 = __importDefault(require("express-validator"));
  const bluebird_1 = __importDefault(require("bluebird"));
  const secrets_1 = require("./util/secrets");
  const Messages_1 = __importDefault(require("./models/Messages"));
  const MongoStore = connect_mongo_1.default(express_session_1.default);
  //Load environment variables from .env file, where API keys and passwords are configured
  dotenv_1.default.config({ path: ".env.example" });
  // Controllers (route handlers)
  const homeController = __importStar(require("./controllers/home"));
  const userController = __importStar(require("./controllers/user"));
  const apiController = __importStar(require("./controllers/api"));
  const contactController = __importStar(require("./controllers/contact"));
  const http = __importStar(require("http"));
  const socketIO = require('socket.io');
  // API keys and Passport configuration
  const passportConfig = __importStar(require("./config/passport"));
  // Create Express server
  const app = express_1.default();
  const server = http.createServer(app);
  const io = socketIO(server);
  // Connect to MongoDB
  const mongoUrl = secrets_1.MONGODB_URI;
  mongoose_1.default.Promise = bluebird_1.default;
  mongoose_1.default.connect(mongoUrl, { useMongoClient: true }).then(() => { }).catch(err => {
      console.log("MongoDB connection error. Please make sure MongoDB is running. " + err);
      // process.exit();
  });
  app.set("views", path_1.default.join(__dirname, "../views"));
  app.set("view engine", "ejs");
  app.use(express_ejs_layouts_1.default);
  app.use(compression_1.default());
  app.use(body_parser_1.default.json());
  app.use(body_parser_1.default.urlencoded({ extended: true }));
  app.use(express_validator_1.default());
  app.use(express_session_1.default({
      resave: true,
      saveUninitialized: true,
      secret: secrets_1.SESSION_SECRET,
      store: new MongoStore({
          url: mongoUrl,
          autoReconnect: true
      })
  }));
  app.use(passport_1.default.initialize());
  app.use(passport_1.default.session());
  app.use(express_flash_1.default());
  app.use(lusca_1.default.xframe("SAMEORIGIN"));
  app.use(lusca_1.default.xssProtection(true));
  app.use((req, res, next) => {
      res.locals.user = req.user;
      next();
  });
  app.use((req, res, next) => {
      // After successful login, redirect back to the intended page
      if (!req.user &&
          req.path !== "/login" &&
          req.path !== "/signup" &&
          !req.path.match(/^\/auth/) &&
          !req.path.match(/\./)) {
          req.session.returnTo = req.path;
      }
      else if (req.user &&
          req.path == "/account") {
          req.session.returnTo = req.path;
      }
      next();
  });
  app.use(express_1.default.static(path_1.default.join(__dirname, "public"), { maxAge: 31557600000 }));
  io.on('connection', function (socket) {
      console.log('User connected');
      socket.on("message", (message) => __awaiter(this, void 0, void 0, function* () {
          io.emit('receiveMessage', message);
          try {
              var chat = new Messages_1.default(message);
              yield chat.save();
          }
          catch (error) {
              console.error(error);
          }
      }));
      socket.on('disconnect', () => {
          console.log('user disconnected');
      });
  });
  /**
   * API examples routes.
   */
  app.get("/api", apiController.getApi);
  app.get("/test", apiController.getApis);
  app.get("/chat/:userid", apiController.getChat);
  app.get("/", homeController.index);
  app.get("/login", userController.getLogin);
  app.post("/login", userController.postLogin);
  app.get("/logout", userController.logout);
  app.get("/forgot", userController.getForgot);
  app.post("/forgot", userController.postForgot);
  app.get("/getDetail", userController.getReset);
  app.post("/reset/:token", userController.postReset);
  app.get("/signup", userController.getSignup);
  app.post("/signup", userController.postSignup);
  app.get("/contact", contactController.getContact);
  app.post("/contact", contactController.postContact);
  app.get("/account", passportConfig.isAuthenticated, userController.getAccount);
  app.post("/account/profile", passportConfig.isAuthenticated, userController.postUpdateProfile);
  app.post("/account/password", passportConfig.isAuthenticated, userController.postUpdatePassword);
  app.post("/account/delete", passportConfig.isAuthenticated, userController.postDeleteAccount);
  app.get("/account/unlink/:provider", passportConfig.isAuthenticated, userController.getOauthUnlink);
  server.listen(4000, function () {
      console.log('listening on *:4000');
  });
  exports.default = app;
  //# sourceMappingURL=app.js.map