fix: site name hot-reloads from DB on every page load, no restart needed

This commit is contained in:
2026-07-16 05:49:22 +08:00
parent f423e1f791
commit 4d2dade44e

View File

@@ -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() ? '' : '<script>location.hash="#/install"</script>';
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;
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() ? '' : '<script>location.hash="#/install"</script>';
html = html.replace('</head>', `<script>window.__API_KEY__=${JSON.stringify(key)};window.__SITE_NAME__=${JSON.stringify(siteName)}</script>${redirect}</head>`);
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); });