Fix jwt-token

This commit is contained in:
2025-09-07 12:38:16 +02:00
parent 33306a5967
commit 6bbbf9855b
6 changed files with 725 additions and 4 deletions

View File

@@ -0,0 +1,121 @@
const express = require('express');
const router = express.Router();
const DeviceHealthService = require('../services/deviceHealthService');
// Global instance (will be initialized on server start)
let deviceHealthService = null;
// Initialize the service reference
const initializeHealthService = (service) => {
deviceHealthService = service;
};
// Get device health service status
router.get('/status', (req, res) => {
try {
if (!deviceHealthService) {
return res.status(503).json({
success: false,
message: 'Device health service not initialized'
});
}
const status = deviceHealthService.getStatus();
res.json({
success: true,
data: {
...status,
checkIntervalMinutes: status.checkInterval / 60000,
offlineThresholdMinutes: status.offlineThreshold / 60000
}
});
} catch (error) {
console.error('Error getting device health status:', error);
res.status(500).json({
success: false,
message: 'Failed to get device health status',
error: process.env.NODE_ENV === 'development' ? error.message : 'Internal server error'
});
}
});
// Start device health monitoring
router.post('/start', (req, res) => {
try {
if (!deviceHealthService) {
return res.status(503).json({
success: false,
message: 'Device health service not initialized'
});
}
deviceHealthService.start();
res.json({
success: true,
message: 'Device health monitoring started'
});
} catch (error) {
console.error('Error starting device health service:', error);
res.status(500).json({
success: false,
message: 'Failed to start device health service',
error: process.env.NODE_ENV === 'development' ? error.message : 'Internal server error'
});
}
});
// Stop device health monitoring
router.post('/stop', (req, res) => {
try {
if (!deviceHealthService) {
return res.status(503).json({
success: false,
message: 'Device health service not initialized'
});
}
deviceHealthService.stop();
res.json({
success: true,
message: 'Device health monitoring stopped'
});
} catch (error) {
console.error('Error stopping device health service:', error);
res.status(500).json({
success: false,
message: 'Failed to stop device health service',
error: process.env.NODE_ENV === 'development' ? error.message : 'Internal server error'
});
}
});
// Trigger manual health check
router.post('/check', async (req, res) => {
try {
if (!deviceHealthService) {
return res.status(503).json({
success: false,
message: 'Device health service not initialized'
});
}
await deviceHealthService.checkDeviceHealth();
res.json({
success: true,
message: 'Manual health check completed'
});
} catch (error) {
console.error('Error running manual health check:', error);
res.status(500).json({
success: false,
message: 'Failed to run health check',
error: process.env.NODE_ENV === 'development' ? error.message : 'Internal server error'
});
}
});
module.exports = { router, initializeHealthService };

View File

@@ -7,6 +7,7 @@ const userRoutes = require('./user');
const alertRoutes = require('./alert');
const dashboardRoutes = require('./dashboard');
const healthRoutes = require('./health');
const { router: deviceHealthRoutes } = require('./deviceHealth');
const debugRoutes = require('./debug');
const detectorsRoutes = require('./detectors');
const detectionsRoutes = require('./detections');
@@ -18,6 +19,7 @@ router.use('/v1/users', userRoutes);
router.use('/v1/alerts', alertRoutes);
router.use('/v1/dashboard', dashboardRoutes);
router.use('/v1/health', healthRoutes);
router.use('/v1/device-health', deviceHealthRoutes);
router.use('/v1/detectors', detectorsRoutes);
router.use('/v1/detections', detectionsRoutes);
router.use('/v1/drone-types', droneTypesRoutes);
@@ -28,6 +30,7 @@ router.use('/users', userRoutes);
router.use('/alerts', alertRoutes);
router.use('/dashboard', dashboardRoutes);
router.use('/health', healthRoutes);
router.use('/device-health', deviceHealthRoutes);
router.use('/debug', debugRoutes);
router.use('/detectors', detectorsRoutes);
router.use('/detections', detectionsRoutes);
@@ -46,6 +49,7 @@ router.get('/', (req, res) => {
alerts: '/api/alerts',
dashboard: '/api/dashboard',
health: '/api/health',
'device-health': '/api/device-health',
'drone-types': '/api/drone-types'
},
documentation: '/api/docs'