const express = require('express')
require('dotenv').config();
const path = require('path');
const morgan = require('morgan');
const livereload = require('livereload');
const connectLivereload = require('connect-livereload');
const connectDB = require('./middleware/database');


// Setup livereload
const liveReloadServer = livereload.createServer();
liveReloadServer.watch(__dirname + "public"); // Watch public directory

const app = express()
connectDB();

// Use morgan to log requests in 'combined' format (includes status code, method, URL, etc.)
app.use(morgan('combined'));

// Middleware to inject livereload script
app.use(connectLivereload());

// Serve static files
app.use(express.static('public'));

// Set the views directory to "src/views"
app.set('views', path.join(__dirname, 'views'));
// Set the view engine (e.g., EJS)
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
    res.render('index.ejs')
})

const port = process.env.PORT;
app.listen(port, () => console.log("Server is running on http://localhost:%i", port));