const fs = require('fs');
const path = require('path');

const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const compression = require('compression');
const bodyParser = require('body-parser');

// backend
const backend = async (app, corsOptions) => {
    app.set('protocol', process.env.PROTOCOL);
    app.set('host', process.env.HOST);
    app.set('port', process.env.PORT_BACKEND);

    app.use(
        helmet.contentSecurityPolicy({
            directives: {
                ...helmet.contentSecurityPolicy.getDefaultDirectives(),
                'script-src': ["'self'", "'unsafe-inline'", "'unsafe-eval'", "cdnjs.cloudflare.com"],
                'connect-src': ["'self'", "ws://127.0.0.1:*", "ws://"+process.env.HOST+":*"],
            }
        }),
        helmet.hidePoweredBy()
    );
    app.use(cors(corsOptions));
    app.use(compression());
    app.use(bodyParser.json({ limit: '50mb' }));
    app.use(bodyParser.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }));

    const testFolder = path.join(process.cwd(), 'test');

    // test files
    app.use('/test', express.static(testFolder));
};

module.exports = backend;
