const express = require("express");
const app = express();
const cors = require("cors");
const morgan = require("morgan");
const port = process.env.PORT || 3132;
const dotenv = require("dotenv");
const path = require("path");

dotenv.config();
console.log(process.env.NODE_ENV);
// database connection

// import routers
const filesRouters = require("./routes/fileRoutes");

// usages
app.use(cors({ origin: "*", methods: "GET,HEAD,PUT,PATCH,POST,DELETE" }));
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use("/uploads", express.static(path.join(__dirname, "./uploads")));

app.use("/files", filesRouters);

// status path
app.get("/status", (req, res) => {
  res.status(200).json({
    success: true,
    message: `media server is running on port ${port} on v-0.1.1`,
  });
});

// 404 path
app.get("*", (req, res) => {
  res.status(404).json({
    success: false,
    message: `no data found for current path.`,
  });
});

app.listen(port, (e) => {
  // console.log(e);
  console.log(`media server is started on port ${port} on v-0.1.0`);
});
