const express = require("express");
const app = express();
const server = require("http").createServer(app);
const mongoose = require("mongoose");
const configs = require("./configs");
const chalk = require("chalk");
const helmet = require("helmet");
const morgan = require("morgan");
const cors = require("cors");

app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(helmet());

// Logs
app.use(morgan("dev"));

app.use(cors());
// Connect Mongoose
mongoose
  .connect(configs.mongodburl, {
    autoIndex: true
  })
  .then(() => {
    console.log(chalk.cyanBright.bold("Mongodb is live!"));
  })
  .catch((error) => {
    console.log("Mongodb error: " + error.message);
  });

// Statik dosyaların servis edilmesi
app.use(express.static("build"));
// Device Set Route
app.use("/api/get", require("./route/device.get.route"));
// Save Route
app.use("/api/save", require("./route/save.route"));
// Device Set Route
app.use("/api/set", require("./route/device.set.route"));
// Device Log Route
app.use("/api/device-log", require("./route/device.log.route"));
// Root public route
// app.use("*", (req, res) => {
//   res.sendStatus(400);
// });
// Server Listening
server.listen(configs.port, () => {
  console.log(chalk.yellowBright.bold.red("Listening: " + configs.port));
});
// Error Handling
app.on("eror", (error) => {
  console.log(error);
});
