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'));
const multer = require('multer');
const forms = multer({limits: { fieldSize: 25 * 1024 * 1024 }});

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({ limit: '50mb', extended: true }));
//app.use(bodyParser.json({limit:'50mb'}));
app.use(forms.array()); 
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') {
    console.log('production');
    app.set('trust proxy', true); // trust first proxy
    if (process.env.PROTOCOL === 'https') {
        console.log("https");
        sess.cookie.secure = true; // serve secure cookies
    } else {
        console.log("http");
        sess.cookie.secure = false; // 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}!`)
})
