perf: only add middleware if relativePath is set

isekai-main
Barış Soner Uşaklı 3 years ago
parent a3bdb70617
commit 41db9436e8

@ -59,19 +59,6 @@ middleware.ensureLoggedIn = (req, res, next) => {
setImmediate(next); setImmediate(next);
}; };
middleware.ensureRelativePath = (req, res, next) => {
const prefix = nconf.get('relative_path');
if (!prefix) {
return setImmediate(next);
}
if (!req.path.startsWith(prefix)) {
return controllers.helpers.redirect(res, req.path);
}
return next();
};
Object.assign(middleware, { Object.assign(middleware, {
admin: require('./admin'), admin: require('./admin'),
...require('./header'), ...require('./header'),

@ -137,8 +137,14 @@ function setupExpressApp(app) {
const compression = require('compression'); const compression = require('compression');
app.use(compression()); app.use(compression());
} }
if (relativePath) {
app.use(middleware.ensureRelativePath); app.use((req, res, next) => {
if (!req.path.startsWith(relativePath)) {
return require('./controllers/helpers').redirect(res, req.path);
}
next();
});
}
app.get(`${relativePath}/ping`, pingController.ping); app.get(`${relativePath}/ping`, pingController.ping);
app.get(`${relativePath}/sping`, pingController.ping); app.get(`${relativePath}/sping`, pingController.ping);

Loading…
Cancel
Save