Initial commit

This commit is contained in:
Wieland Schöbl
2019-08-20 13:46:25 +02:00
commit 8f62b0faa0
16 changed files with 4370 additions and 0 deletions

68
src/cli.ts Normal file
View File

@@ -0,0 +1,68 @@
/*
* 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 {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 * as commander from 'commander';
import {readFileSync} from 'fs';
import {join, resolve} from 'path';
import {MinimalPlugin} from './plugin/minimal-plugin';
const pluginVersion = JSON.parse(
readFileSync(join(__dirname, '..', 'package.json'))
.toString(),
).version;
process.on('unhandledRejection', (error) => {
throw error;
});
commander
.version(pluginVersion)
.command('run')
.option('-b <backenUrl>', 'URL of the StApps backend deployment', 'http://localhost:3000')
.option('-n <name>', 'The name of the plugin', 'minimal-plugin') // TODO: adjust default
.option('-r <routeName>', 'The name of the route', 'minimalPlugin') // TODO: adjust default
.option('-u <url>', 'The url of the plugin', 'http://localhost') // TODO: adjust default
// tslint:disable-next-line:no-magic-numbers
.option('-p <port>', 'The port of the plugin', 4000) // TODO: adjust default
.action(async (backendUrl: string, name: string, routeName: string, url: string, port: number) => {
// create an instance of the PluginClient
const pluginClient = new PluginClient(new HttpClient(), backendUrl);
// create an instance of your plugin
const plugin = new MinimalPlugin(port, name, url, `/${routeName}`, 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', // the name of the request interface TODO: adjust
'SCMinimalResponse', // the name of the response interface TODO: adjust
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('done.');
})
.catch((err: Error) => {
throw err;
});
});
commander.parse(process.argv);

16
src/common.ts Normal file
View 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

View 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);
}
}

View 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[];
}

View 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;
}