[bug] Refactor admin panel routing; Fix bug 77576

This commit is contained in:
Sergey Konovalov
2025-10-15 17:24:14 +03:00
parent 318d81c02f
commit dcea07c4c0
9 changed files with 276 additions and 119 deletions

View File

@ -92,6 +92,20 @@ const cfgDownloadMaxBytes = config.get('FileConverter.converter.maxDownloadBytes
const app = express();
app.disable('x-powered-by');
// Enable CORS in development mode for AdminPanel webpack dev server
if (process.env.NODE_ENV.startsWith('development-')) {
const cors = require('cors');
app.use(
cors({
origin: true,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
})
);
}
//path.resolve uses __dirname by default(unexpected path in pkg)
app.set('views', path.resolve(process.cwd(), cfgHtmlTemplate));
app.set('view engine', 'ejs');
@ -162,44 +176,6 @@ docsCoServer.install(server, app, () => {
);
});
// Proxy AdminPanel endpoints for testing
if (process.env.NODE_ENV.startsWith('development-')) {
/**
* Simple proxy to localhost:9000 for testing AdminPanel routes
* @param {string} pathPrefix - Path to prepend or empty string to strip mount path
*/
const proxyToAdmin =
(pathPrefix = '') =>
(req, res) => {
const targetPath = pathPrefix + req.url;
const options = {
hostname: 'localhost',
port: 9000,
path: targetPath,
method: req.method,
headers: {...req.headers, host: 'localhost:9000'}
};
const proxyReq = http.request(options, proxyRes => {
res.status(proxyRes.statusCode);
Object.entries(proxyRes.headers).forEach(([key, value]) => res.setHeader(key, value));
proxyRes.pipe(res);
});
proxyReq.on('error', () => res.sendStatus(502));
req.pipe(proxyReq);
};
app.use('/api/v1/admin', proxyToAdmin('/api/v1/admin'));
app.all('/admin', (req, res, next) => {
if (req.path === '/admin' && !req.path.endsWith('/')) {
return res.redirect(302, '/admin/');
}
next();
});
app.use('/admin', proxyToAdmin());
}
app.get('/index.html', (req, res) => {
return co(function* () {
const ctx = new operationContext.Context();