const PORT = 80;
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const session = require('express-session')
const fileUpload = require('express-fileupload');
const config = require("./config");
const markdown = require('markdown-it')({html: true})
                    .use(require('markdown-it-attrs'));

app.locals.markdown = (filename) => { 
    const path = process.cwd() + filename;
    const data = require('fs').readFileSync(path, 'utf8').toString();
    return markdown.render(data);
}
                    
// Configure the EJS template engine
// Add support for markdown file rendering
app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/static', express.static('public'));
app.use(fileUpload());

const ONE_HOUR = 3600000;
const ONE_MINUTE = 1000*60*60;//Testing purpose
const sess= {
    secret: 'AIFEX super secret',
    resave: false,
    saveUninitialized: true,
    maxAge: ONE_HOUR,
    cookie: {}
};

if (app.get('env') === 'production') {
    app.set('trust proxy', true); // trust first proxy
    sess.cookie.secure = true; // serve secure cookies
}

app.use(session(sess));

require("./routes/account.js")(app, config);
require("./routes/statics.js")(app, config);
require("./routes/session.js")(app, config);
require("./routes/website.js")(app, config);

app.listen(PORT, function () {
    console.log(`Server is running on port ${PORT}!`)
})
