forked from viossat/alertmanager-discord
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
68 lines (54 loc) · 1.83 KB
/
index.js
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
// Simple Discord webhook proxy for Alertmanager
const Koa = require("koa");
const yaml = require("js-yaml");
const fs = require("fs");
const winston = require("winston");
const { router } = require("./router");
const { cleanSecrets } = require("./secrets");
const port = process.env.PORT || 5001;
const configPath = "/etc/alertmanager-discord.yml";
const hookRegExp = new RegExp("https://discord(?:app)?.com/api/webhooks/[0-9]+/[a-zA-Z0-9_-]+");
if (require.main === module) {
let config,
routes = {},
webhookTokens = [];
try {
config = yaml.load(fs.readFileSync(configPath));
} catch (err) {
console.error("Failed to read configuration file:", err.message);
}
const webhookSearchPattern = "/api/webhooks/";
if (config !== undefined && config.hooks !== undefined && Array.isArray(config.hooks)) {
for (let route of config.hooks) {
if (!route.hook || !route.hook.startsWith || !hookRegExp.test(route.hook)) {
console.warn("Not a valid discord web hook for slug =", route.slug);
continue;
}
routes[route.slug] = route.hook;
const webhookPatternIndex = route.hook.indexOf(webhookSearchPattern);
const webhookToken = route.hook.substring(webhookPatternIndex + webhookSearchPattern.length);
webhookTokens.push(webhookToken);
}
}
const logFormatter = winston.format.combine(
cleanSecrets({ secrets: webhookTokens }),
winston.format.json(),
);
const transport = new winston.transports.Console({
format: logFormatter,
});
const logger = winston.createLogger({
transports: [transport],
});
const app = new Koa();
app.context.routes = routes;
app.context.logger = logger;
app.use(router.routes());
app.listen(port, (err) => {
if (err) {
logger.error(err.stack);
return;
}
logger.info("Listening on port " + port);
});
}