mcp-cve-intelligence-server-lite-test
Version:
Lite Model Context Protocol server for comprehensive CVE intelligence gathering with multi-source exploit discovery, designed for security professionals and cybersecurity researchers - Alpha Release
148 lines • 4.98 kB
JavaScript
import { createContextLogger } from '../utils/logger.js';
import { getAppConfiguration } from '../config/index.js';
const logger = createContextLogger('HealthService');
export class HealthService {
startTime;
requestCount = 0;
sourceManager;
version;
environment;
constructor(sourceManager, version, environment) {
this.startTime = Date.now();
this.sourceManager = sourceManager;
const config = getAppConfiguration();
this.version = version || config.server.version;
this.environment = environment || config.server.environment;
}
/**
* Increment request counter for metrics
*/
incrementRequestCount() {
this.requestCount++;
}
/**
* Get comprehensive health status
*/
async getHealth() {
try {
const uptime = Date.now() - this.startTime;
const sourcesHealth = await this.getSourcesHealth();
const memoryUsage = process.memoryUsage();
const status = sourcesHealth.status === 'healthy' ? 'healthy' : 'degraded';
return {
status,
uptime,
version: this.version,
environment: this.environment,
lastChecked: new Date().toISOString(),
dependencies: {
sources: sourcesHealth,
},
metrics: {
memory: {
used: memoryUsage.heapUsed,
total: memoryUsage.heapTotal,
percentage: Math.round((memoryUsage.heapUsed / memoryUsage.heapTotal) * 100),
},
activeConnections: 0, // Simplified for lite version
totalRequests: this.requestCount,
},
};
}
catch (error) {
logger.error('Health check failed', error);
return {
status: 'unhealthy',
uptime: Date.now() - this.startTime,
version: this.version,
environment: this.environment,
lastChecked: new Date().toISOString(),
dependencies: {
sources: { status: 'unhealthy', lastChecked: new Date().toISOString() },
},
metrics: {
memory: { used: 0, total: 0, percentage: 0 },
activeConnections: 0,
totalRequests: this.requestCount,
},
};
}
}
/**
* Check readiness (can handle requests)
*/
async isReady() {
try {
if (!this.sourceManager) {
return false;
}
const sourcesHealth = await this.getSourcesHealth();
return sourcesHealth.status !== 'unhealthy';
}
catch (error) {
logger.error('Readiness check failed', error);
return false;
}
}
/**
* Check liveness (application is running)
*/
async isAlive() {
return true;
// try {
// // Basic liveness check - if we can execute this method, we're alive
// return true;
// } catch (error) {
// logger.error('Liveness check failed', error as Error);
// return false;
// }
}
/**
* Get sources health status
*/
async getSourcesHealth() {
try {
if (!this.sourceManager) {
return {
status: 'unhealthy',
lastChecked: new Date().toISOString(),
details: { error: 'Source manager not initialized' },
};
}
const startTime = Date.now();
const health = await this.sourceManager.getSourceHealth();
const responseTime = Date.now() - startTime;
const healthyCount = Object.values(health).filter(h => h.status === 'available').length;
const totalCount = Object.keys(health).length;
let status;
if (healthyCount === totalCount) {
status = 'healthy';
}
else if (healthyCount > 0) {
status = 'degraded';
}
else {
status = 'unhealthy';
}
return {
status,
lastChecked: new Date().toISOString(),
responseTime,
details: {
healthyCount,
totalCount,
sources: health,
},
};
}
catch (error) {
logger.error('Sources health check failed', error);
return {
status: 'unhealthy',
lastChecked: new Date().toISOString(),
details: { error: error.message },
};
}
}
}
//# sourceMappingURL=health-service.js.map