添加Valkey支持

This commit is contained in:
CN-JS-HuiBai
2026-04-05 15:02:19 +08:00
parent 484a7a766f
commit e7b8000808
4 changed files with 128 additions and 27 deletions

View File

@@ -137,8 +137,29 @@ app.post('/api/setup/test', async (req, res) => {
}
});
app.post('/api/setup/test-valkey', async (req, res) => {
const { host, port, password } = req.body;
try {
const Redis = require('ioredis');
const redis = new Redis({
host: host || 'localhost',
port: parseInt(port) || 6379,
password: password || undefined,
lazyConnect: true,
maxRetriesPerRequest: 1,
connectTimeout: 5000
});
await redis.connect();
await redis.ping();
await redis.disconnect();
res.json({ success: true, message: 'Valkey connection successful' });
} catch (err) {
res.status(400).json({ success: false, error: err.message });
}
});
app.post('/api/setup/init', async (req, res) => {
const { host, port, user, password, database } = req.body;
const { host, port, user, password, database, vHost, vPort, vPassword } = req.body;
try {
const mysql = require('mysql2/promise');
const connection = await mysql.createConnection({
@@ -211,6 +232,9 @@ MYSQL_PORT=${port || '3306'}
MYSQL_USER=${user || 'root'}
MYSQL_PASSWORD=${password || ''}
MYSQL_DATABASE=${dbName}
VALKEY_HOST=${vHost || 'localhost'}
VALKEY_PORT=${vPort || '6379'}
VALKEY_PASSWORD=${vPassword || ''}
PORT=${process.env.PORT || 3000}
HOST=${process.env.HOST || '0.0.0.0'}
REFRESH_INTERVAL=${process.env.REFRESH_INTERVAL || 5000}
@@ -223,9 +247,13 @@ REFRESH_INTERVAL=${process.env.REFRESH_INTERVAL || 5000}
process.env.MYSQL_USER = user;
process.env.MYSQL_PASSWORD = password;
process.env.MYSQL_DATABASE = dbName;
process.env.VALKEY_HOST = vHost;
process.env.VALKEY_PORT = vPort;
process.env.VALKEY_PASSWORD = vPassword;
// Re-initialize pool
// Re-initialize pools
db.initPool();
cache.init();
isDbInitialized = true;
res.json({ success: true, message: 'Initialization complete' });
@@ -607,6 +635,10 @@ app.get('/api/metrics/overview', async (req, res) => {
// Get network traffic history (past 24h) from Prometheus
app.get('/api/metrics/network-history', async (req, res) => {
try {
const cacheKey = 'network_history_all';
const cached = await cache.get(cacheKey);
if (cached) return res.json(cached);
const [sources] = await db.query('SELECT * FROM prometheus_sources');
if (sources.length === 0) {
return res.json({ timestamps: [], rx: [], tx: [] });
@@ -625,6 +657,7 @@ app.get('/api/metrics/network-history', async (req, res) => {
}
const merged = prometheusService.mergeNetworkHistories(validHistories);
await cache.set(cacheKey, merged, 300); // Cache for 5 minutes
res.json(merged);
} catch (err) {
console.error('Error fetching network history history:', err);