76 lines
2.0 KiB
JavaScript
76 lines
2.0 KiB
JavaScript
|
const express = require('express');
|
||
|
const router = express.Router();
|
||
|
const { initializeMonitor, cleanupMonitor } = require('../modules/cleanup');
|
||
|
|
||
|
// 存储监控服务的状态
|
||
|
let monitorStatus = {
|
||
|
isInitialized: false,
|
||
|
domainId: null,
|
||
|
lastError: null
|
||
|
};
|
||
|
|
||
|
// 初始化监控服务
|
||
|
router.post('/initialize', async (req, res) => {
|
||
|
try {
|
||
|
const { domainId } = req.body;
|
||
|
|
||
|
if (!domainId) {
|
||
|
return res.status(400).json({ error: '缺少必要的domainId参数' });
|
||
|
}
|
||
|
|
||
|
if (monitorStatus.isInitialized) {
|
||
|
return res.status(400).json({ error: '监控服务已经初始化' });
|
||
|
}
|
||
|
|
||
|
const result = initializeMonitor(domainId);
|
||
|
if (result && result.includes('失败')) {
|
||
|
monitorStatus.lastError = result;
|
||
|
return res.status(500).json({ error: result });
|
||
|
}
|
||
|
|
||
|
monitorStatus.isInitialized = true;
|
||
|
monitorStatus.domainId = domainId;
|
||
|
monitorStatus.lastError = null;
|
||
|
|
||
|
res.json({
|
||
|
message: '监控服务初始化成功',
|
||
|
status: monitorStatus
|
||
|
});
|
||
|
} catch (error) {
|
||
|
console.error('初始化监控服务失败:', error);
|
||
|
monitorStatus.lastError = error.message;
|
||
|
res.status(500).json({ error: '初始化监控服务失败', message: error.message });
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// 清理监控服务
|
||
|
router.post('/cleanup', async (req, res) => {
|
||
|
try {
|
||
|
if (!monitorStatus.isInitialized) {
|
||
|
return res.status(400).json({ error: '监控服务未初始化' });
|
||
|
}
|
||
|
|
||
|
cleanupMonitor();
|
||
|
monitorStatus = {
|
||
|
isInitialized: false,
|
||
|
domainId: null,
|
||
|
lastError: null
|
||
|
};
|
||
|
|
||
|
res.json({
|
||
|
message: '监控服务清理成功',
|
||
|
status: monitorStatus
|
||
|
});
|
||
|
} catch (error) {
|
||
|
console.error('清理监控服务失败:', error);
|
||
|
monitorStatus.lastError = error.message;
|
||
|
res.status(500).json({ error: '清理监控服务失败', message: error.message });
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// 获取监控服务状态
|
||
|
router.get('/status', (req, res) => {
|
||
|
res.json(monitorStatus);
|
||
|
});
|
||
|
|
||
|
module.exports = router;
|