-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (60 loc) · 2.29 KB
/
Copy pathindex.js
File metadata and controls
71 lines (60 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const { loadConfig } = require('./lib/configLoader');
const HealthChecker = require('./lib/healthChecker');
const { createRouter } = require('./lib/middleware');
/**
* Initialize catOrNot health monitoring
* @param {string|object} configPathOrObject - Path to config file or config object
* @returns {Router} Express router with health endpoint
*/
function catOrNot(configPathOrObject) {
let config;
if (typeof configPathOrObject === 'string') {
// Load config from file
config = loadConfig(configPathOrObject);
} else if (typeof configPathOrObject === 'object') {
// Use provided config object
const { validateConfig } = require('./lib/configLoader');
validateConfig(configPathOrObject);
config = {
endpoints: configPathOrObject.endpoints || [],
checkInterval: configPathOrObject.checkInterval || 30000,
timeout: configPathOrObject.timeout || 5000,
healthPath: configPathOrObject.healthPath || '/health',
enableMonitoring: configPathOrObject.enableMonitoring !== false,
catImagePath: configPathOrObject.catImagePath || require('path').join(__dirname, 'cat.jpeg')
};
} else {
throw new Error('Config must be a file path string or configuration object');
}
// Create health checker
const healthChecker = new HealthChecker(config);
// Start monitoring if enabled
if (config.enableMonitoring) {
healthChecker.startMonitoring();
}
// Create and return router
const router = createRouter(healthChecker, config);
// Attach health checker to router for manual control
router.healthChecker = healthChecker;
router.config = config;
return router;
}
/**
* Create a standalone health checker without Express router
* @param {string|object} configPathOrObject - Path to config file or config object
* @returns {HealthChecker} Health checker instance
*/
function createHealthChecker(configPathOrObject) {
let config;
if (typeof configPathOrObject === 'string') {
config = loadConfig(configPathOrObject);
} else {
const { validateConfig } = require('./lib/configLoader');
validateConfig(configPathOrObject);
config = configPathOrObject;
}
return new HealthChecker(config);
}
module.exports = catOrNot;
module.exports.createHealthChecker = createHealthChecker;
module.exports.HealthChecker = HealthChecker;