import { Plugin, Logger, APIServer } from '@ale-run/runtime';
import { ExampleMetricDriver } from './metric';
import path from 'node:path';
const logger = Logger.getLogger('plugin:example');
export default class ExamplePlugin extends Plugin {
public async activate(): Promise<void> {
logger.info(`plugin ${this.name} is activate`, this.options);
// regist app
const catalog = await this.context.getCatalog();
await catalog.regist(`${path.resolve(__dirname, 'app/example')}`);
await catalog.registPreset(`${path.resolve(__dirname, 'app/example')}`);
// add api route
const api: APIServer = this.get('api-server');
if (!api) throw new Error(`api-server is required`);
const router = api.routers.body.get(this.name);
router.post('/example', async (req, res) => {
res.send({
say: 'Hello'
});
});
// add metric driver
const clustermgr = this.context.getClusterManager();
clustermgr.addMetricDriver('example', ExampleMetricDriver);
}
public async deactivate(): Promise<void> {
logger.info(`plugin ${this.name} will be deactivate`);
const server: APIServer = this.get('api-server');
server?.routers?.body?.remove(this.name);
// remove metric driver
const clustermgr = this.context.getClusterManager();
clustermgr.removeMetricDriver('example');
}
}