const express = require("express");
const cors = require("cors");

const app = express();

var corsOptions = {
  origin: ["http://localhost:4200", "http://64.23.254.43/"]
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(express.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));

const db = require("./app/models");

db.sequelize.sync()
  .then(() => {
    console.log("Synced db.");
  })
  .catch((err) => {
    console.log("Failed to sync db: " + err.message);
  });

// simple route
app.get("/", (req, res) => {
  res.json({ message: "Welcome to E-learning application." });
});

require("./app/routes/users.routes")(app);
require("./app/routes/authy.routes")(app);
require("./app/routes/loginlogs.routes")(app);
require("./app/routes/self_paced.routes")(app);
require("./app/routes/testlogs.routes")(app);
require("./app/routes/points.routes")(app);
require("./app/routes/enroll.routes")(app);
require("./app/routes/acronyms.routes")(app);
require("./app/routes/disscussion.routes")(app);
require("./app/routes/knowledgeass.routes")(app);
require("./app/routes/meeting.routes")(app);
require("./app/routes/mindmap.routes")(app);
require("./app/routes/notification.routes")(app);
require("./app/routes/practiceans.routes")(app);
require("./app/routes/userlogs.routes")(app);
require("./app/routes/video.routes")(app);
require("./app/routes/adminlogs.routes")(app);
require("./app/routes/certificate.routes")(app);
require("./app/routes/client.routes")(app);
require("./app/routes/cohort.routes")(app);
require("./app/routes/domain.routes")(app);
require("./app/routes/emailtemplate.routes")(app);
require("./app/routes/flashcard.routes")(app);
require("./app/routes/live_events.routes")(app);
require("./app/routes/mini_masterclass.routes")(app);
require("./app/routes/reference.routes")(app);
require("./app/routes/studyMaterial.routes")(app);
require("./app/routes/subdomain.routes")(app);
require("./app/routes/topic.routes")(app);
require("./app/routes/support.routes")(app);
require("./app/routes/template.routes")(app);
require("./app/routes/variable.routes")(app);

// set port, listen for requests
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});
