mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 00:23:03 +00:00
refactor: move minimal-plugin to monorepo
This commit is contained in:
82
examples/minimal-plugin/src/cli.ts
Normal file
82
examples/minimal-plugin/src/cli.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2019-2021 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {HttpClient} from '@openstapps/api/lib/http-client';
|
||||
import {PluginClient} from '@openstapps/api/lib/plugin-client';
|
||||
import {Converter} from '@openstapps/core-tools/lib/schema';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {Command, Option} from 'commander';
|
||||
import {readFileSync} from 'fs';
|
||||
import {join, resolve} from 'path';
|
||||
import {MinimalPlugin} from './plugin/minimal-plugin';
|
||||
|
||||
|
||||
process.on('unhandledRejection', (error) => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
const pluginVersion = JSON.parse(
|
||||
readFileSync(join(__dirname, '..', 'package.json'))
|
||||
.toString(),
|
||||
).version;
|
||||
|
||||
const program = new Command()
|
||||
.version(pluginVersion)
|
||||
// tslint:disable: newline-per-chained-call
|
||||
.addOption(new Option('-b, --backend-url <string>', 'URL of the StApps backend deployment').default('http://localhost:3000').env('STAPPS_BACKEND'))
|
||||
.addOption(new Option('-n, --plugin-name <string>', 'The name of the plugin').default('minimal-plugin')) // TODO: adjust default
|
||||
.addOption(new Option('-r, --route-name <string>', 'The name of the route').default('minimalplugin')) // TODO: adjust default
|
||||
.addOption(new Option('-u, --url <string>', 'The url of the plugin').default('http://localhost').env('PLUGIN_URL')) // TODO: adjust default
|
||||
.addOption(new Option('-p, --port <number>', 'The port of the plugin').default('4000').env('PLUGIN_PORT')) // TODO: adjust default
|
||||
.parse(process.argv);
|
||||
// tslint:enable: newline-per-chained-call
|
||||
|
||||
const options = program.opts();
|
||||
|
||||
// create an instance of the PluginClient
|
||||
const pluginClient = new PluginClient(new HttpClient(), options.backendUrl);
|
||||
|
||||
// create an instance of your plugin
|
||||
const plugin = new MinimalPlugin(
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
Number.parseInt(options.port, 10), options.pluginName, options.url, `/${options.routeName}`, options.backendUrl,
|
||||
new Converter(resolve(__dirname, '..', 'src', 'plugin', 'protocol')), // an instance of the converter. Required
|
||||
// because your requests and response schemas are defined in the plugin. The path should lead to your request and
|
||||
// response interfaces
|
||||
'SCMinimalRequest', // TODO: adjust name of the request interface
|
||||
'SCMinimalResponse', // TODO: adjust name of the response interface
|
||||
JSON.parse(readFileSync(resolve(__dirname, '..', 'package.json'))
|
||||
.toString()).version, // get the version of the plugin from the package.json
|
||||
);
|
||||
|
||||
pluginClient.registerPlugin(plugin)
|
||||
.then(() => {
|
||||
Logger.ok(`Successfully registered plugin '${options.pluginName}' on /${options.routeName} .`);
|
||||
})
|
||||
.catch((err: Error) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach((signal) => {
|
||||
process.once(signal as NodeJS.Signals, () => {
|
||||
pluginClient.unregisterPlugin(plugin)
|
||||
.then(() => {
|
||||
Logger.ok(`Successfully unregistered plugin '${options.pluginName}' from /${options.routeName} .`);
|
||||
})
|
||||
.catch((err: Error) => {
|
||||
throw err;
|
||||
});
|
||||
}) ;
|
||||
});
|
||||
16
examples/minimal-plugin/src/common.ts
Normal file
16
examples/minimal-plugin/src/common.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// TODO: place commonly used values here
|
||||
67
examples/minimal-plugin/src/plugin/minimal-plugin.ts
Normal file
67
examples/minimal-plugin/src/plugin/minimal-plugin.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Plugin} from '@openstapps/api/lib/plugin';
|
||||
import * as express from 'express';
|
||||
import {SCMinimalRequest} from './protocol/request';
|
||||
import {SCMinimalResponse} from './protocol/response';
|
||||
|
||||
/**
|
||||
* The Plugin Class
|
||||
*
|
||||
* This is where all of your logic should take place
|
||||
* TODO: rename the class
|
||||
*/
|
||||
export class MinimalPlugin extends Plugin {
|
||||
|
||||
/**
|
||||
* Calculates the sum of a list of numbers
|
||||
*
|
||||
* TODO: remove this function and write your own ones
|
||||
*
|
||||
* @param numbers the list of numbers
|
||||
*/
|
||||
private static sum(numbers: number[]): number {
|
||||
let out = 0;
|
||||
|
||||
for (const num of numbers) {
|
||||
out += num;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The method that gets called when its route is being invoked
|
||||
*
|
||||
* For this example the whole purpose of the plugin is to receive a list of numbers and return the sum of them.
|
||||
* TODO: remove the body of the function and replace with your own logic
|
||||
*
|
||||
* @param req the express request object
|
||||
* @param res the express response object
|
||||
*/
|
||||
// tslint:disable-next-line:prefer-function-over-method
|
||||
protected async onRouteInvoke(req: express.Request, res: express.Response): Promise<void> {
|
||||
// get the body from the request as a SCMinimalRequest for static type-safety
|
||||
const requestBody = req.body as SCMinimalRequest;
|
||||
|
||||
// create out response body
|
||||
const responseBody: SCMinimalResponse = {
|
||||
sum: MinimalPlugin.sum(requestBody.numbers),
|
||||
};
|
||||
|
||||
// send the response
|
||||
res.json(responseBody);
|
||||
}
|
||||
}
|
||||
30
examples/minimal-plugin/src/plugin/protocol/request.ts
Normal file
30
examples/minimal-plugin/src/plugin/protocol/request.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Request Interface
|
||||
*
|
||||
* All incoming requests will look like this, this is being checked by the backend. You need to add the @validatable tag
|
||||
* like shown below for the plugin to work. The request can have any layout you like.
|
||||
* TODO: remove body of the interface and replace with your own layout
|
||||
*
|
||||
* @validatable
|
||||
*/
|
||||
export interface SCMinimalRequest {
|
||||
/**
|
||||
* The list of numbers to add
|
||||
*/
|
||||
numbers: number[];
|
||||
}
|
||||
30
examples/minimal-plugin/src/plugin/protocol/response.ts
Normal file
30
examples/minimal-plugin/src/plugin/protocol/response.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Request Interface
|
||||
*
|
||||
* All your responses to the backend are required to look like this. You need to add the @validatable tag like shown
|
||||
* below for the plugin to work. The response can have any layout you like.
|
||||
* TODO: remove body of the interface and replace with your own layout
|
||||
*
|
||||
* @validatable
|
||||
*/
|
||||
export interface SCMinimalResponse {
|
||||
/**
|
||||
* The sum of the numbers from the request
|
||||
*/
|
||||
sum: number;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "./node_modules/@openstapps/configuration/tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user