Files
pronature-platform/backend/app/WebServer.js
T
2024-11-27 18:10:09 +02:00

122 lines
3.9 KiB
JavaScript

import express from 'express'
import session from 'express-session';
import compression from 'compression';
import MongoDBStore from 'connect-mongodb-session';
import https from 'https';
import fs from 'fs';
import cookieParser from 'cookie-parser';
import helmet from 'helmet';
import { v4 as uuidv4 } from 'uuid';
/**
* The Web Server class, manages all request from the web platform, manages the APIs
*/
class WebServer {
name = 'webServer';
/**
* Initializes the web server plugin
* @param {App} app The application instance
*/
async init(app) {
const xapp = express();
this.xapp = xapp;
xapp.disable('x-powered-by');
xapp.use(compression());
xapp.use(cookieParser());
const store = new MongoDBStore(session)({
uri: app.config.db.url,
databaseName: app.config.db.name,
collection: 'user_sessions'
});
store.on('error', function (error) {
console.log(error);
});
xapp.use(session({
secret: app.config.am.cookie.secret,
cookie: {
maxAge: app.config.am.cookie.maxAge || 1000 * 60 * 60 * 24 * 7,
secure: !!app.config.am.cookie.secure,
httpOnly: true,
sameSite: app.config.am.cookie.sameSite
},
store,
resave: false,
saveUninitialized: (app.config.am?.session?.saveUninitialized === undefined) ? true : app.config.am.session.saveUninitialized,
proxy: true
}));
// xapp.use((req, res, next) => {
// let l = app.config.langs.find(l => l.code == (req.query?.lang || req.cookies.lang || 'bg'));
// req.lang = l || app.config.langs[0];
// next();
// })
xapp.use(express.json({ limit: '150mb' }));
xapp.use(express.urlencoded({ extended: false, limit: '150mb' }));
xapp.use((req, res, next) => {
res.locals.cspNonce = uuidv4();
next();
});
app.config.am.helmet && xapp.use(helmet(app.config.am.helmet));
}
/**
* Starts the web server plugin
* @param {App} app The application instance
*/
async start(app) {
let indexFile = app.root + '/index.html';
let indexFileContent = fs.readFileSync(indexFile, { encoding: 'utf-8' });
function index(req, res) {
//res.sendFile(indexFile);
res.send(indexFileContent.replace(/\#NONCE\#/g, res.locals.cspNonce));
}
this.xapp.get('/', index);
// app.config.langs.forEach(l => {
// this.xapp.use('/' + l.code, index);
// })
this.xapp.use(express.static(`${this.app.root}/dist/`));
this.xapp.use((req, res, next) => {
if (req.method == 'GET' || req.method == 'POST') {
return res.status(404).end();
} else next();
});
///error handler!
this.xapp.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
let started = () => {
console.log(`app started on port ${app.config.site.port}. SSL is ${app.config.site.ssl ? 'enabled' : 'disabled'}.`)
}
if (app.config.site.ssl) {
this.server = https.createServer({
key: fs.readFileSync(app.config.site.certificate.key),
cert: fs.readFileSync(app.config.site.certificate.cert),
passphrase: app.config.site.certificate.passphrase
}, this.xapp).listen(app.config.site.port, started);
} else {
this.server = this.xapp.listen(app.config.site.port, app.config.site.hostAddress || '127.0.0.1', started);
}
}
}
export { WebServer };