fix: site name hot-reloads from DB on every page load, no restart needed
This commit is contained in:
@@ -17,32 +17,30 @@ const HTML_PATH = path.join(PUBLIC_DIR, 'index.html');
|
|||||||
|
|
||||||
let cachedHtml = null;
|
let cachedHtml = null;
|
||||||
let htmlTimestamp = 0;
|
let htmlTimestamp = 0;
|
||||||
let cachedSiteName = 'MC举报系统';
|
|
||||||
|
|
||||||
function getSiteName() { return cachedSiteName; }
|
function getSiteName() {
|
||||||
|
if (!isInstalled()) return 'MC举报系统';
|
||||||
async function refreshSiteName() {
|
|
||||||
if (!isInstalled()) return;
|
|
||||||
try {
|
try {
|
||||||
const { getPool } = require('./db');
|
const { getPool } = require('./db');
|
||||||
const p = await getPool();
|
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举报系统');
|
||||||
const [rows] = await p.execute("SELECT v FROM settings WHERE k='site_name'");
|
} catch { return 'MC举报系统'; }
|
||||||
if (rows.length > 0) cachedSiteName = rows[0].v;
|
|
||||||
} catch {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHtmlWithKey() {
|
function getHtmlWithKey() {
|
||||||
|
return getSiteName().then(siteName => {
|
||||||
const stat = fs.statSync(HTML_PATH);
|
const stat = fs.statSync(HTML_PATH);
|
||||||
const mtime = stat.mtimeMs;
|
const mtime = stat.mtimeMs;
|
||||||
if (cachedHtml && htmlTimestamp >= mtime) return cachedHtml;
|
if (!cachedHtml || htmlTimestamp < mtime || cachedHtml.indexOf(siteName) === -1) {
|
||||||
let html = fs.readFileSync(HTML_PATH, 'utf-8');
|
let html = fs.readFileSync(HTML_PATH, 'utf-8');
|
||||||
const cfg = getConfig();
|
const cfg = getConfig();
|
||||||
const key = cfg?.api_key || '';
|
const key = cfg?.api_key || '';
|
||||||
const redirect = isInstalled() ? '' : '<script>location.hash="#/install"</script>';
|
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>`);
|
html = html.replace('</head>', `<script>window.__API_KEY__=${JSON.stringify(key)};window.__SITE_NAME__=${JSON.stringify(siteName)}</script>${redirect}</head>`);
|
||||||
cachedHtml = html;
|
cachedHtml = html;
|
||||||
htmlTimestamp = mtime;
|
htmlTimestamp = mtime;
|
||||||
return html;
|
}
|
||||||
|
return cachedHtml;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
app.use(helmet({
|
app.use(helmet({
|
||||||
@@ -127,15 +125,15 @@ if (isInstalled()) {
|
|||||||
|
|
||||||
app.get('/api/health', (req, res) => res.json({ status: 'ok', installed: 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.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: '接口不存在' });
|
if (req.path.startsWith('/api')) return res.status(404).json({ error: '接口不存在' });
|
||||||
res.setHeader('Cache-Control', 'no-cache');
|
res.setHeader('Cache-Control', 'no-cache');
|
||||||
res.type('html').send(getHtmlWithKey());
|
res.type('html').send(await getHtmlWithKey());
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use((err, req, res, next) => {
|
app.use((err, req, res, next) => {
|
||||||
@@ -148,6 +146,6 @@ app.use((err, req, res, next) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
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`);
|
else console.log(`System not installed. Visit http://localhost:${PORT}/#/install`);
|
||||||
}).on('error', (err) => { console.error('Failed to start:', err.message); process.exit(1); });
|
}).on('error', (err) => { console.error('Failed to start:', err.message); process.exit(1); });
|
||||||
|
|||||||
Reference in New Issue
Block a user