优化白色主题渲染逻辑

This commit is contained in:
CN-JS-HuiBai
2026-04-04 19:25:39 +08:00
parent 824ead4bee
commit 2d46729c27
3 changed files with 74 additions and 8 deletions

View File

@@ -276,7 +276,49 @@ app.use(async (req, res, next) => {
next();
});
app.use(express.static(path.join(__dirname, '..', 'public')));
// Helper to serve index.html with injected settings
const serveIndex = async (req, res) => {
try {
const indexPath = path.join(__dirname, '..', 'public', 'index.html');
if (!fs.existsSync(indexPath)) return res.status(404).send('Not found');
let html = fs.readFileSync(indexPath, 'utf8');
// Fetch settings
let settings = {
page_name: '数据可视化展示大屏',
title: '数据可视化展示大屏',
logo_url: null,
default_theme: 'dark'
};
if (isDbInitialized) {
try {
const [rows] = await db.query('SELECT * FROM site_settings WHERE id = 1');
if (rows.length > 0) settings = rows[0];
} catch (e) {
// DB not ready or table missing
}
}
// Inject settings
const settingsJson = JSON.stringify(settings);
const injection = `<script>window.SITE_SETTINGS = ${settingsJson};</script>`;
// Replace <head> with <head> + injection
html = html.replace('<head>', '<head>' + injection);
res.send(html);
} catch (err) {
console.error('Error serving index:', err);
res.status(500).send('Internal Server Error');
}
};
app.get('/', serveIndex);
app.get('/index.html', serveIndex);
app.use(express.static(path.join(__dirname, '..', 'public'), { index: false }));
// ==================== Prometheus Source CRUD ====================
@@ -553,8 +595,9 @@ app.get('/api/metrics/cpu-history', async (req, res) => {
});
// SPA fallback
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
app.get('*', (req, res, next) => {
if (req.path.startsWith('/api/') || req.path.includes('.')) return next();
serveIndex(req, res);
});