import express from 'express';
import session from 'express-session';
import cookieParser from 'cookie-parser';
import path from 'path';

import ApiRouter from './routers/api.js';
import UserRouter from './routers/user.js';
import startChat from './messages.js';
import config from './config.js';

const app = express();

app.use(session({
    secret: config.secret,
    resave: true,
    saveUninitialized: false,
    cookie: { maxAge: 1000 * 60 * 60 * 24 }
}));
app.use(express.static(path.join('./public')));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cookieParser());

app.set('view engine', 'pug');

app.use('/api', ApiRouter);
app.use('/', UserRouter);

const server = app.listen(config.app_port, () => {
    console.log("App has been started!");

    if(config.enable_chat) {
        startChat();
    }
});

export default server;
