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

const bodyparser = require("body-parser");
const fileUpload = require("express-fileupload");
const path = require("path");
const session = require('express-session');

require("dotenv").config();

const adminroute = require("./routes/bgv_admin");
const userroute = require("./routes/user");
const candidateroute = require("./routes/candidate_admin");

// Body Parser
app.use(bodyparser.urlencoded({ extended: true }));
app.use(bodyparser.json());

// File Upload
app.use(fileUpload());

// Public Folder Access
app.use(express.static(path.join(__dirname, "public")));

// View Engine
app.set("view engine", "ejs");



app.use(session({
    secret: 'vsearch_bgv_secret',
    resave: false,
    saveUninitialized: false,
    cookie: {
        maxAge: 24 * 60 * 60 * 1000
    }
}));


app.use(function (req, res, next) {

    res.locals.user = req.session.user || null;

    next();

});


// Routes
app.use("/", userroute);
app.use("/bgv_admin", adminroute);
app.use("/candidate_admin", candidateroute);

// Server
app.listen(3000, () => {
    console.log("Server Running on http://localhost:3000");
});