添加实时刷新机制

This commit is contained in:
CN-JS-HuiBai
2026-04-05 15:06:27 +08:00
parent 2149aa0208
commit d7f8db89a3

View File

@@ -293,7 +293,15 @@ app.post('/api/setup/admin', async (req, res) => {
const hash = crypto.pbkdf2Sync(password, salt, 1000, 64, 'sha512').toString('hex');
await db.query('INSERT INTO users (username, password, salt) VALUES (?, ?, ?)', [username, hash, salt]);
res.json({ success: true, message: 'Admin account created' });
const [userRows] = await db.query('SELECT id, username FROM users WHERE username = ?', [username]);
const user = userRows[0];
// Auto-login after creation so the next setup steps (like adding Prometheus) work without 401
const sessionId = crypto.randomBytes(32).toString('hex');
sessions.set(sessionId, { id: user.id, username: user.username });
res.setHeader('Set-Cookie', `session_id=${sessionId}; Path=/; HttpOnly; SameSite=Strict; Max-Age=86400`);
res.json({ success: true, message: 'Admin account created and logged in' });
} catch (err) {
console.error('Admin creation error:', err);
res.status(500).json({ error: err.message });
@@ -414,6 +422,9 @@ app.post('/api/sources', requireAuth, async (req, res) => {
);
const [rows] = await db.query('SELECT * FROM prometheus_sources WHERE id = ?', [result.insertId]);
// Clear network history cache to force refresh
await cache.del('network_history_all');
res.status(201).json(rows[0]);
} catch (err) {
console.error('Error adding source:', err);
@@ -430,6 +441,9 @@ app.put('/api/sources/:id', requireAuth, async (req, res) => {
'UPDATE prometheus_sources SET name = ?, url = ?, description = ? WHERE id = ?',
[name, url, description || '', req.params.id]
);
// Clear network history cache
await cache.del('network_history_all');
const [rows] = await db.query('SELECT * FROM prometheus_sources WHERE id = ?', [req.params.id]);
res.json(rows[0]);
} catch (err) {
@@ -442,6 +456,8 @@ app.put('/api/sources/:id', requireAuth, async (req, res) => {
app.delete('/api/sources/:id', requireAuth, async (req, res) => {
try {
await db.query('DELETE FROM prometheus_sources WHERE id = ?', [req.params.id]);
// Clear network history cache
await cache.del('network_history_all');
res.json({ message: 'Source deleted' });
} catch (err) {
console.error('Error deleting source:', err);