fix: dynamic site name from DB, displayed in header/title/sidebar

This commit is contained in:
2026-07-16 04:02:19 +08:00
parent abf747e3af
commit f423e1f791
3 changed files with 22 additions and 5 deletions

View File

@@ -17,6 +17,19 @@ const HTML_PATH = path.join(PUBLIC_DIR, 'index.html');
let cachedHtml = null;
let htmlTimestamp = 0;
let cachedSiteName = 'MC举报系统';
function getSiteName() { return cachedSiteName; }
async function refreshSiteName() {
if (!isInstalled()) return;
try {
const { getPool } = require('./db');
const p = await getPool();
const [rows] = await p.execute("SELECT v FROM settings WHERE k='site_name'");
if (rows.length > 0) cachedSiteName = rows[0].v;
} catch {}
}
function getHtmlWithKey() {
const stat = fs.statSync(HTML_PATH);
@@ -26,7 +39,7 @@ function getHtmlWithKey() {
const cfg = getConfig();
const key = cfg?.api_key || '';
const redirect = isInstalled() ? '' : '<script>location.hash="#/install"</script>';
html = html.replace('</head>', `<script>window.__API_KEY__=${JSON.stringify(key)}</script>${redirect}</head>`);
html = html.replace('</head>', `<script>window.__API_KEY__=${JSON.stringify(key)};window.__SITE_NAME__=${JSON.stringify(cachedSiteName)}</script>${redirect}</head>`);
cachedHtml = html;
htmlTimestamp = mtime;
return html;
@@ -135,6 +148,6 @@ app.use((err, req, res, next) => {
});
app.listen(PORT, () => {
if (isInstalled()) console.log(`Server running at http://localhost:${PORT}`);
if (isInstalled()) { refreshSiteName().then(() => console.log(`Server running at http://localhost:${PORT}`)); }
else console.log(`System not installed. Visit http://localhost:${PORT}/#/install`);
}).on('error', (err) => { console.error('Failed to start:', err.message); process.exit(1); });