initial
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
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';
|
||||
|
||||
class WebServer {
|
||||
name = 'webServer';
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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 };
|
||||
Reference in New Issue
Block a user