From 4d2dade44e85983acdcdd4c917bd6f44d5480aea Mon Sep 17 00:00:00 2001 From: canglan Date: Thu, 16 Jul 2026 05:49:22 +0800 Subject: [PATCH] fix: site name hot-reloads from DB on every page load, no restart needed --- backend/server.js | 48 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/backend/server.js b/backend/server.js index 3544b96..947a0bd 100644 --- a/backend/server.js +++ b/backend/server.js @@ -17,32 +17,30 @@ 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; +function getSiteName() { + if (!isInstalled()) return 'MC举报系统'; 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 {} + return getPool().then(p => p.execute("SELECT v FROM settings WHERE k='site_name'")).then(([rows]) => rows.length > 0 ? rows[0].v : 'MC举报系统').catch(() => 'MC举报系统'); + } catch { return 'MC举报系统'; } } function getHtmlWithKey() { - const stat = fs.statSync(HTML_PATH); - const mtime = stat.mtimeMs; - if (cachedHtml && htmlTimestamp >= mtime) return cachedHtml; - let html = fs.readFileSync(HTML_PATH, 'utf-8'); - const cfg = getConfig(); - const key = cfg?.api_key || ''; - const redirect = isInstalled() ? '' : ''; - html = html.replace('', `${redirect}`); - cachedHtml = html; - htmlTimestamp = mtime; - return html; + return getSiteName().then(siteName => { + const stat = fs.statSync(HTML_PATH); + const mtime = stat.mtimeMs; + if (!cachedHtml || htmlTimestamp < mtime || cachedHtml.indexOf(siteName) === -1) { + let html = fs.readFileSync(HTML_PATH, 'utf-8'); + const cfg = getConfig(); + const key = cfg?.api_key || ''; + const redirect = isInstalled() ? '' : ''; + html = html.replace('', `${redirect}`); + cachedHtml = html; + htmlTimestamp = mtime; + } + return cachedHtml; + }); } app.use(helmet({ @@ -127,15 +125,15 @@ if (isInstalled()) { app.get('/api/health', (req, res) => res.json({ status: 'ok', installed: isInstalled() })); -app.get('/', (req, res) => { +app.get('/', async (req, res) => { res.setHeader('Cache-Control', 'no-cache'); - res.type('html').send(getHtmlWithKey()); + res.type('html').send(await getHtmlWithKey()); }); -app.get('*', (req, res) => { +app.get('*', async (req, res) => { if (req.path.startsWith('/api')) return res.status(404).json({ error: '接口不存在' }); res.setHeader('Cache-Control', 'no-cache'); - res.type('html').send(getHtmlWithKey()); + res.type('html').send(await getHtmlWithKey()); }); app.use((err, req, res, next) => { @@ -148,6 +146,6 @@ app.use((err, req, res, next) => { }); app.listen(PORT, () => { - if (isInstalled()) { refreshSiteName().then(() => console.log(`Server running at http://localhost:${PORT}`)); } + if (isInstalled()) 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); });