Commit 74988908 authored by nanahira's avatar nanahira

use multi source

parent c1eb5ffc
......@@ -10,7 +10,7 @@
"license": "MIT",
"dependencies": {
"koishi-target-def": "^1.0.0",
"koishi-thirdeye": "^10.0.20",
"koishi-thirdeye": "^10.0.25",
"lodash": "^4.17.21"
},
"devDependencies": {
......@@ -5281,9 +5281,9 @@
}
},
"node_modules/koishi-thirdeye": {
"version": "10.0.20",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.20.tgz",
"integrity": "sha512-aYBiQ6R4A8VcZ7xw0qfECIJvWsuEoW3MWdBH+SuWi0dcrwhvfF3naEtkC0ZGEnWOkauyW1YyOzTK/Fieikmv4w==",
"version": "10.0.25",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.25.tgz",
"integrity": "sha512-bw75VAHYD5CgN6xpnwEUG148nP9KhYiRx17gJBmIhOB53Ur62aCiAgYaU2YixUKWE7bVsoPmQQQBFdi1HZZkOw==",
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......@@ -5291,7 +5291,7 @@
"koishi-decorators": "^2.0.1",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.10",
"schemastery-gen": "^3.1.13",
"typed-reflector": "^1.0.10"
},
"peerDependencies": {
......@@ -6421,9 +6421,9 @@
}
},
"node_modules/schemastery-gen": {
"version": "3.1.10",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.10.tgz",
"integrity": "sha512-hwCLOICGEodCcQt+eLoEEvNhm7iSEwJB37JQ+27cHbggTry/Rq07ivXVsPw63Et74EobEaOGDVOTzJ4kIQa01w==",
"version": "3.1.13",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.13.tgz",
"integrity": "sha512-KXkjV9AuKWBlkJ8yEhLYDh7wRcM8p/M+s30Ig6g5eYlGfv7KVUGmpSVVtcH+fUZG+zS/7/UfCreMqqqFJBPdNw==",
"dependencies": {
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
......@@ -11796,9 +11796,9 @@
}
},
"koishi-thirdeye": {
"version": "10.0.20",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.20.tgz",
"integrity": "sha512-aYBiQ6R4A8VcZ7xw0qfECIJvWsuEoW3MWdBH+SuWi0dcrwhvfF3naEtkC0ZGEnWOkauyW1YyOzTK/Fieikmv4w==",
"version": "10.0.25",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.25.tgz",
"integrity": "sha512-bw75VAHYD5CgN6xpnwEUG148nP9KhYiRx17gJBmIhOB53Ur62aCiAgYaU2YixUKWE7bVsoPmQQQBFdi1HZZkOw==",
"requires": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......@@ -11806,7 +11806,7 @@
"koishi-decorators": "^2.0.1",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.10",
"schemastery-gen": "^3.1.13",
"typed-reflector": "^1.0.10"
}
},
......@@ -12631,9 +12631,9 @@
}
},
"schemastery-gen": {
"version": "3.1.10",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.10.tgz",
"integrity": "sha512-hwCLOICGEodCcQt+eLoEEvNhm7iSEwJB37JQ+27cHbggTry/Rq07ivXVsPw63Et74EobEaOGDVOTzJ4kIQa01w==",
"version": "3.1.13",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.13.tgz",
"integrity": "sha512-KXkjV9AuKWBlkJ8yEhLYDh7wRcM8p/M+s30Ig6g5eYlGfv7KVUGmpSVVtcH+fUZG+zS/7/UfCreMqqqFJBPdNw==",
"requires": {
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
......
......@@ -45,7 +45,7 @@
},
"dependencies": {
"koishi-target-def": "^1.0.0",
"koishi-thirdeye": "^10.0.20",
"koishi-thirdeye": "^10.0.25",
"lodash": "^4.17.21"
},
"peerDependencies": {
......
......@@ -6,7 +6,7 @@ import { AlertDto } from './def';
import { SendTarget } from 'koishi-target-def';
@RegisterSchema()
export class Instance {
export class InstanceConfig {
@SchemaProperty({
required: true,
description: 'POST 的 Authorization Bearer 字段,用于标识不同路由。',
......@@ -72,21 +72,9 @@ export class Instance {
@RegisterSchema()
export class AlertmanagerPluginConfig {
constructor(config: AlertmanagerPluginConfigLike) {}
@SchemaProperty({
description: '请求路径。',
default: '/alertmanager',
})
path: string;
@SchemaProperty({ type: Instance })
instances: Instance[];
getInstance(header: string) {
const token = header?.startsWith('Bearer ') ? header.slice(7) : header;
return this.instances.find((instance) => instance.token === token);
}
}
export type AlertmanagerPluginConfigLike = Partial<AlertmanagerPluginConfig>;
// import 'source-map-support/register';
import { AlertmanagerPluginConfig } from './config';
import { AlertmanagerPluginConfig, InstanceConfig } from './config';
import {
DefinePlugin,
BasePlugin,
Post,
StarterPlugin,
Inject,
LifecycleEvents,
InjectLogger,
MultiInstancePlugin,
} from 'koishi-thirdeye';
import { Adapter, Logger, Router } from 'koishi';
import _ from 'lodash';
import { AlertDto } from './def';
export * from './config';
@DefinePlugin({ name: 'alertmanager', schema: AlertmanagerPluginConfig })
@DefinePlugin()
export class Instance extends StarterPlugin(InstanceConfig) {
@Inject()
private bots: Adapter.BotList;
private async send(content: string) {
return _.flatten(
await Promise.all(
this.config.targets.map((target) => target.send(this.bots, content)),
),
);
}
trigger(body: AlertDto) {
const content = this.config.formatAlert(body);
return this.send(content);
}
isMatch(token: string) {
return this.config.token === token;
}
}
@DefinePlugin()
export default class AlertmanagerPlugin
extends BasePlugin<AlertmanagerPluginConfig>
extends MultiInstancePlugin(Instance, AlertmanagerPluginConfig)
implements LifecycleEvents
{
@Inject(true)
......@@ -25,9 +50,14 @@ export default class AlertmanagerPlugin
@InjectLogger()
private logger: Logger;
private getInstance(header: string) {
const token = header?.startsWith('Bearer ') ? header.slice(7) : header;
return this.instances.find((instance) => instance.isMatch(token));
}
onApply() {
this.router.post(this.config.path, async (ctx) => {
const instance = this.config.getInstance(ctx.headers['authorization']);
const instance = this.getInstance(ctx.headers['authorization']);
if (!instance) {
ctx.body = {
status: 'error',
......@@ -36,8 +66,8 @@ export default class AlertmanagerPlugin
ctx.status = 403;
return;
}
this.logger.warn(`${JSON.stringify(ctx.request.body)}`);
await instance.send(this.bots, instance.formatAlert(ctx.request.body));
this.logger.warn(JSON.stringify(ctx.request.body));
await instance.trigger(ctx.request.body);
ctx.body = {
status: 'success',
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment