Commit 74988908 authored by nanahira's avatar nanahira

use multi source

parent c1eb5ffc
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"koishi-target-def": "^1.0.0", "koishi-target-def": "^1.0.0",
"koishi-thirdeye": "^10.0.20", "koishi-thirdeye": "^10.0.25",
"lodash": "^4.17.21" "lodash": "^4.17.21"
}, },
"devDependencies": { "devDependencies": {
...@@ -5281,9 +5281,9 @@ ...@@ -5281,9 +5281,9 @@
} }
}, },
"node_modules/koishi-thirdeye": { "node_modules/koishi-thirdeye": {
"version": "10.0.20", "version": "10.0.25",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.20.tgz", "resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.25.tgz",
"integrity": "sha512-aYBiQ6R4A8VcZ7xw0qfECIJvWsuEoW3MWdBH+SuWi0dcrwhvfF3naEtkC0ZGEnWOkauyW1YyOzTK/Fieikmv4w==", "integrity": "sha512-bw75VAHYD5CgN6xpnwEUG148nP9KhYiRx17gJBmIhOB53Ur62aCiAgYaU2YixUKWE7bVsoPmQQQBFdi1HZZkOw==",
"dependencies": { "dependencies": {
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11", "@types/koa__router": "^8.0.11",
...@@ -5291,7 +5291,7 @@ ...@@ -5291,7 +5291,7 @@
"koishi-decorators": "^2.0.1", "koishi-decorators": "^2.0.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.10", "schemastery-gen": "^3.1.13",
"typed-reflector": "^1.0.10" "typed-reflector": "^1.0.10"
}, },
"peerDependencies": { "peerDependencies": {
...@@ -6421,9 +6421,9 @@ ...@@ -6421,9 +6421,9 @@
} }
}, },
"node_modules/schemastery-gen": { "node_modules/schemastery-gen": {
"version": "3.1.10", "version": "3.1.13",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.10.tgz", "resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.13.tgz",
"integrity": "sha512-hwCLOICGEodCcQt+eLoEEvNhm7iSEwJB37JQ+27cHbggTry/Rq07ivXVsPw63Et74EobEaOGDVOTzJ4kIQa01w==", "integrity": "sha512-KXkjV9AuKWBlkJ8yEhLYDh7wRcM8p/M+s30Ig6g5eYlGfv7KVUGmpSVVtcH+fUZG+zS/7/UfCreMqqqFJBPdNw==",
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
...@@ -11796,9 +11796,9 @@ ...@@ -11796,9 +11796,9 @@
} }
}, },
"koishi-thirdeye": { "koishi-thirdeye": {
"version": "10.0.20", "version": "10.0.25",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.20.tgz", "resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.25.tgz",
"integrity": "sha512-aYBiQ6R4A8VcZ7xw0qfECIJvWsuEoW3MWdBH+SuWi0dcrwhvfF3naEtkC0ZGEnWOkauyW1YyOzTK/Fieikmv4w==", "integrity": "sha512-bw75VAHYD5CgN6xpnwEUG148nP9KhYiRx17gJBmIhOB53Ur62aCiAgYaU2YixUKWE7bVsoPmQQQBFdi1HZZkOw==",
"requires": { "requires": {
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11", "@types/koa__router": "^8.0.11",
...@@ -11806,7 +11806,7 @@ ...@@ -11806,7 +11806,7 @@
"koishi-decorators": "^2.0.1", "koishi-decorators": "^2.0.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.10", "schemastery-gen": "^3.1.13",
"typed-reflector": "^1.0.10" "typed-reflector": "^1.0.10"
} }
}, },
...@@ -12631,9 +12631,9 @@ ...@@ -12631,9 +12631,9 @@
} }
}, },
"schemastery-gen": { "schemastery-gen": {
"version": "3.1.10", "version": "3.1.13",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.10.tgz", "resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.13.tgz",
"integrity": "sha512-hwCLOICGEodCcQt+eLoEEvNhm7iSEwJB37JQ+27cHbggTry/Rq07ivXVsPw63Et74EobEaOGDVOTzJ4kIQa01w==", "integrity": "sha512-KXkjV9AuKWBlkJ8yEhLYDh7wRcM8p/M+s30Ig6g5eYlGfv7KVUGmpSVVtcH+fUZG+zS/7/UfCreMqqqFJBPdNw==",
"requires": { "requires": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
}, },
"dependencies": { "dependencies": {
"koishi-target-def": "^1.0.0", "koishi-target-def": "^1.0.0",
"koishi-thirdeye": "^10.0.20", "koishi-thirdeye": "^10.0.25",
"lodash": "^4.17.21" "lodash": "^4.17.21"
}, },
"peerDependencies": { "peerDependencies": {
......
...@@ -6,7 +6,7 @@ import { AlertDto } from './def'; ...@@ -6,7 +6,7 @@ import { AlertDto } from './def';
import { SendTarget } from 'koishi-target-def'; import { SendTarget } from 'koishi-target-def';
@RegisterSchema() @RegisterSchema()
export class Instance { export class InstanceConfig {
@SchemaProperty({ @SchemaProperty({
required: true, required: true,
description: 'POST 的 Authorization Bearer 字段,用于标识不同路由。', description: 'POST 的 Authorization Bearer 字段,用于标识不同路由。',
...@@ -72,21 +72,9 @@ export class Instance { ...@@ -72,21 +72,9 @@ export class Instance {
@RegisterSchema() @RegisterSchema()
export class AlertmanagerPluginConfig { export class AlertmanagerPluginConfig {
constructor(config: AlertmanagerPluginConfigLike) {}
@SchemaProperty({ @SchemaProperty({
description: '请求路径。', description: '请求路径。',
default: '/alertmanager', default: '/alertmanager',
}) })
path: string; 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 'source-map-support/register';
import { AlertmanagerPluginConfig } from './config'; import { AlertmanagerPluginConfig, InstanceConfig } from './config';
import { import {
DefinePlugin, DefinePlugin,
BasePlugin, StarterPlugin,
Post,
Inject, Inject,
LifecycleEvents, LifecycleEvents,
InjectLogger, InjectLogger,
MultiInstancePlugin,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Adapter, Logger, Router } from 'koishi'; import { Adapter, Logger, Router } from 'koishi';
import _ from 'lodash';
import { AlertDto } from './def';
export * from './config'; 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 export default class AlertmanagerPlugin
extends BasePlugin<AlertmanagerPluginConfig> extends MultiInstancePlugin(Instance, AlertmanagerPluginConfig)
implements LifecycleEvents implements LifecycleEvents
{ {
@Inject(true) @Inject(true)
...@@ -25,9 +50,14 @@ export default class AlertmanagerPlugin ...@@ -25,9 +50,14 @@ export default class AlertmanagerPlugin
@InjectLogger() @InjectLogger()
private logger: Logger; 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() { onApply() {
this.router.post(this.config.path, async (ctx) => { 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) { if (!instance) {
ctx.body = { ctx.body = {
status: 'error', status: 'error',
...@@ -36,8 +66,8 @@ export default class AlertmanagerPlugin ...@@ -36,8 +66,8 @@ export default class AlertmanagerPlugin
ctx.status = 403; ctx.status = 403;
return; return;
} }
this.logger.warn(`${JSON.stringify(ctx.request.body)}`); this.logger.warn(JSON.stringify(ctx.request.body));
await instance.send(this.bots, instance.formatAlert(ctx.request.body)); await instance.trigger(ctx.request.body);
ctx.body = { ctx.body = {
status: 'success', 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