mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 09:32:41 +00:00
refacator: read configuration from env variables
This commit is contained in:
18
README.md
18
README.md
@@ -1,23 +1,34 @@
|
|||||||
# minimal-connector
|
# minimal-connector
|
||||||
|
|
||||||
## Prerequisites
|
## Prerequisites
|
||||||
|
|
||||||
* `node` and `npm` installed
|
* `node` and `npm` installed
|
||||||
* a backend, which is running locally (on http://localhost:3000)
|
* a backend, which is running locally (e.g. on http://localhost:3000)
|
||||||
|
|
||||||
## How to get started
|
## How to get started
|
||||||
|
|
||||||
To install all required `npm` packages:
|
To install all required `npm` packages:
|
||||||
|
|
||||||
```shell script
|
```shell script
|
||||||
npm install
|
npm install
|
||||||
```
|
```
|
||||||
|
|
||||||
To prepare the script to be executed, run:
|
To prepare the script to be executed, run:
|
||||||
|
|
||||||
```shell script
|
```shell script
|
||||||
npm run build
|
npm run build
|
||||||
```
|
```
|
||||||
|
|
||||||
To start the plugin, you need to execute the code in "CLI" script:
|
To start the plugin, you need to execute the code in "CLI" script:
|
||||||
|
|
||||||
```shell script
|
```shell script
|
||||||
node lib/cli.js run <backendURL> <pluginName> <routeName> <URL> <port>
|
node lib/cli.js run <backendURL> <pluginName> <routeName> <URL> <port>
|
||||||
e.g.:
|
With fallback values being:
|
||||||
node lib/cli.js run http://localhost:3000 minimal-plugin minimalPlugin http://localhost 4000
|
node lib/cli.js run http://localhost:3000 minimal-plugin minimalPlugin http://localhost 4000
|
||||||
```
|
```
|
||||||
|
|
||||||
## Creating your own plugin
|
## Creating your own plugin
|
||||||
|
|
||||||
* Modify the default values in `cli.ts` to your needs:
|
* Modify the default values in `cli.ts` to your needs:
|
||||||
* update the name
|
* update the name
|
||||||
* update the port
|
* update the port
|
||||||
@@ -27,8 +38,11 @@ node lib/cli.js run http://localhost:3000 minimal-plugin minimalPlugin http://lo
|
|||||||
* Delete the `sum` function in `minimal-plugin.ts` and write your own logic inside the `onRouteInvoke` function
|
* Delete the `sum` function in `minimal-plugin.ts` and write your own logic inside the `onRouteInvoke` function
|
||||||
* Define the layout of your http requests and responses inside the `/plugin/protocol` folder. The names of the interfaces
|
* Define the layout of your http requests and responses inside the `/plugin/protocol` folder. The names of the interfaces
|
||||||
need to be the same as the request and response names you defined in the `cli.ts`
|
need to be the same as the request and response names you defined in the `cli.ts`
|
||||||
|
|
||||||
## Code structure
|
## Code structure
|
||||||
|
|
||||||
Folder `src` contains:
|
Folder `src` contains:
|
||||||
|
|
||||||
* Reference implementations for CLI and a plugin
|
* Reference implementations for CLI and a plugin
|
||||||
* [/cli.ts](src/cli.ts)
|
* [/cli.ts](src/cli.ts)
|
||||||
* minimal CLI to start your connector, that uses CLI-args, so that there are no hard coded values for configuration
|
* minimal CLI to start your connector, that uses CLI-args, so that there are no hard coded values for configuration
|
||||||
|
|||||||
30
src/cli.ts
30
src/cli.ts
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2019 StApps
|
* Copyright (C) 2019-2021 StApps
|
||||||
* This program is free software: you can redistribute it and/or modify it
|
* 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
|
* under the terms of the GNU General Public License as published by the Free
|
||||||
* Software Foundation, version 3.
|
* Software Foundation, version 3.
|
||||||
@@ -22,26 +22,38 @@ import {readFileSync} from 'fs';
|
|||||||
import {join, resolve} from 'path';
|
import {join, resolve} from 'path';
|
||||||
import {MinimalPlugin} from './plugin/minimal-plugin';
|
import {MinimalPlugin} from './plugin/minimal-plugin';
|
||||||
|
|
||||||
const pluginVersion = JSON.parse(
|
|
||||||
readFileSync(join(__dirname, '..', 'package.json'))
|
|
||||||
.toString(),
|
|
||||||
).version;
|
|
||||||
|
|
||||||
process.on('unhandledRejection', (error) => {
|
process.on('unhandledRejection', (error) => {
|
||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const pluginVersion = JSON.parse(
|
||||||
|
readFileSync(join(__dirname, '..', 'package.json'))
|
||||||
|
.toString(),
|
||||||
|
).version;
|
||||||
|
|
||||||
const program = new Command()
|
const program = new Command()
|
||||||
.version(pluginVersion)
|
.version(pluginVersion)
|
||||||
.option('-b, --backend-url <string>', 'URL of the StApps backend deployment', 'http://localhost:3000')
|
.option('-b, --backend-url <string>', 'URL of the StApps backend deployment', 'http://localhost:3000')
|
||||||
.option('-n, --plugin-name <string>', 'The name of the plugin', 'minimal-plugin') // TODO: adjust default
|
.option('-n, --plugin-name <string>', 'The name of the plugin', 'minimal-plugin') // TODO: adjust default
|
||||||
.option('-r, --route-name <string>', 'The name of the route', 'minimalPlugin') // TODO: adjust default
|
.option('-r, --route-name <string>', 'The name of the route', 'minimalplugin') // TODO: adjust default
|
||||||
.option('-u, --url <string>', 'The url of the plugin', 'http://localhost') // TODO: adjust default
|
.option('-u, --url <string>', 'The url of the plugin', 'http://localhost') // TODO: adjust default
|
||||||
.option('-p, --port <number>', 'The port of the plugin', '4000') // TODO: adjust default
|
.option('-p, --port <number>', 'The port of the plugin', '4000') // TODO: adjust default
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
|
|
||||||
const options = program.opts();
|
const options = program.opts();
|
||||||
|
|
||||||
|
// read essencial options from ENV variables if available
|
||||||
|
if ('STAPPS_BACKEND' in process.env && typeof process.env.STAPPS_BACKEND === 'string') {
|
||||||
|
options.backendUrl = process.env.STAPPS_BACKEND;
|
||||||
|
}
|
||||||
|
if ('PLUGIN_URL' in process.env && typeof process.env.PLUGIN_URL === 'string') {
|
||||||
|
options.url = process.env.PLUGIN_URL;
|
||||||
|
}
|
||||||
|
if ('PLUGIN_PORT' in process.env && typeof process.env.PLUGIN_PORT === 'string') {
|
||||||
|
options.port = process.env.PLUGIN_PORT;
|
||||||
|
}
|
||||||
|
|
||||||
// create an instance of the PluginClient
|
// create an instance of the PluginClient
|
||||||
const pluginClient = new PluginClient(new HttpClient(), options.backendUrl);
|
const pluginClient = new PluginClient(new HttpClient(), options.backendUrl);
|
||||||
|
|
||||||
@@ -52,15 +64,15 @@ const plugin = new MinimalPlugin(
|
|||||||
new Converter(resolve(__dirname, '..', 'src', 'plugin', 'protocol')), // an instance of the converter. Required
|
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
|
// because your requests and response schemas are defined in the plugin. The path should lead to your request and
|
||||||
// response interfaces
|
// response interfaces
|
||||||
'SCMinimalRequest', // the name of the request interface TODO: adjust
|
'SCMinimalRequest', // TODO: adjust name of the request interface
|
||||||
'SCMinimalResponse', // the name of the response interface TODO: adjust
|
'SCMinimalResponse', // TODO: adjust name of the response interface
|
||||||
JSON.parse(readFileSync(resolve(__dirname, '..', 'package.json'))
|
JSON.parse(readFileSync(resolve(__dirname, '..', 'package.json'))
|
||||||
.toString()).version, // get the version of the plugin from the package.json
|
.toString()).version, // get the version of the plugin from the package.json
|
||||||
);
|
);
|
||||||
|
|
||||||
pluginClient.registerPlugin(plugin)
|
pluginClient.registerPlugin(plugin)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
Logger.ok('done.');
|
Logger.ok(`Successfully registered plugin '${options.pluginName}' on /${options.urlSegment} .`);
|
||||||
})
|
})
|
||||||
.catch((err: Error) => {
|
.catch((err: Error) => {
|
||||||
throw err;
|
throw err;
|
||||||
|
|||||||
Reference in New Issue
Block a user