Skip to content

Commit f63f5a5

Browse files
committed
feat(plugin-web-service-consortium): add dedicated plugin for consortium management API
Some other packages were heavily refactored to make this possible as well. The introduction of the web service plugin was just the beginning of the process of making it possible to host different API endpoints on custom TCP ports with unique configurations that might be necessary in a highly constrained enterprise environment for example. There's a test case specifically designed to showcase and verify the behavior where the consortium API is hosted separately from the rest of the APIs exposed by the cmd-api-server package. Signed-off-by: Peter Somogyvari <[email protected]>
1 parent bb18774 commit f63f5a5

File tree

45 files changed

+2400
-375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2400
-375
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
"build:frontend": "lerna exec --stream --scope '*/*cockpit' -- ng build --prod",
1414
"build:backend": "npm-run-all lint clean generate-sdk tsc webpack",
1515
"build:dev:pkg:cmd-api-server": "lerna exec --stream --scope '*/*api-server' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
16+
"build:dev:pkg:common": "lerna exec --stream --scope '*/*common' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
17+
"build:dev:pkg:core-api": "lerna exec --stream --scope '*/*core-api' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
1618
"build:dev:pkg:test-tooling": "lerna exec --stream --scope '*/*test-tooling' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
1719
"build:dev:pkg:bif-plugin-ledger-connector-quorum": "lerna exec --stream --scope '*/*connector-quorum' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
20+
"build:dev:pkg:bif-plugin-web-service-consortium": "lerna exec --stream --scope '*/*web-service-consortium' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
1821
"build:dev:pkg:sdk": "lerna exec --stream --scope '*/*sdk' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
1922
"webpack": "npm-run-all webpack:web:dev webpack:node:dev webpack:web:prod webpack:node:prod",
2023
"webpack:web:prod": "lerna exec --stream --ignore '*/*{cockpit,server,test-tooling}' -- webpack --env=prod --target=web --config ../../webpack.config.js",

packages/bif-cmd-api-server/package-lock.json

+17-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/bif-cmd-api-server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"@types/compression": "1.7.0",
7171
"@types/convict": "5.2.0",
7272
"@types/cors": "2.8.6",
73-
"@types/express": "4.17.3",
73+
"@types/express": "4.17.6",
7474
"@types/joi": "14.3.4",
7575
"@types/multer": "1.4.2",
7676
"@types/secp256k1": "3.5.3",

packages/bif-cmd-api-server/src/main/typescript/api-server.ts

+30-25
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import express, {
55
Express,
66
Request,
77
Response,
8-
NextFunction,
98
RequestHandler,
109
Application,
1110
} from "express";
@@ -21,8 +20,7 @@ import {
2120
isIPluginWebService,
2221
IPluginWebService,
2322
} from "@hyperledger-labs/bif-core-api";
24-
import { CreateConsortiumEndpointV1 } from "./consortium/routes/create-consortium-endpoint-v1";
25-
import { IBifApiServerOptions, ConfigService } from "./config/config-service";
23+
import { IBifApiServerOptions } from "./config/config-service";
2624
import { BIF_OPEN_API_JSON } from "./openapi-spec";
2725
import { Logger, LoggerProvider } from "@hyperledger-labs/bif-common";
2826
import { Servers } from "./common/servers";
@@ -70,6 +68,21 @@ export class ApiServer {
7068
}
7169

7270
public async shutdown(): Promise<void> {
71+
this.log.info(`Shutting down API server ...`);
72+
const webServicesShutdown = this.options.plugins
73+
.filter((pluginInstance) => isIPluginWebService(pluginInstance))
74+
.map((pluginInstance: ICactusPlugin) => {
75+
return (pluginInstance as IPluginWebService).shutdown();
76+
});
77+
78+
this.log.info(
79+
`Found ${webServicesShutdown.length} web service plugin(s), shutting them down ...`
80+
);
81+
await Promise.all(webServicesShutdown);
82+
this.log.info(
83+
`Shut down ${webServicesShutdown.length} web service plugin(s) OK`
84+
);
85+
7386
if (this.httpServerApi) {
7487
this.log.info(`Closing HTTP server of the API...`);
7588
await Servers.shutdown(this.httpServerApi);
@@ -124,31 +137,23 @@ export class ApiServer {
124137
const openApiValidator = this.createOpenApiValidator();
125138
await openApiValidator.install(app);
126139

127-
app.get(
128-
"/healthcheck",
129-
(req: Request, res: Response, next: NextFunction) => {
130-
res.json({ success: true, timestamp: new Date() });
131-
}
132-
);
133-
134-
const storage: IPluginKVStorage = await this.createStoragePlugin();
135-
const configService = new ConfigService();
136-
const config = configService.getOrCreate();
137-
{
138-
const endpoint = new CreateConsortiumEndpointV1({ storage, config });
139-
app.post(endpoint.getPath(), endpoint.handleRequest.bind(endpoint));
140-
}
140+
const healthcheckHandler = (req: Request, res: Response) => {
141+
res.json({
142+
success: true,
143+
createdAt: new Date(),
144+
memoryUsage: process.memoryUsage(),
145+
});
146+
};
147+
app.get("/api/v1/api-server/healthcheck", healthcheckHandler);
141148

142-
this.options.plugins
149+
this.log.info(`Starting to install web services...`);
150+
const webServicesInstalled = this.options.plugins
143151
.filter((pluginInstance) => isIPluginWebService(pluginInstance))
144-
.forEach((pluginInstance: ICactusPlugin) => {
145-
(pluginInstance as IPluginWebService).installWebService(app);
152+
.map((pluginInstance: ICactusPlugin) => {
153+
return (pluginInstance as IPluginWebService).installWebServices(app);
146154
});
147-
148-
// FIXME
149-
// app.get('/api/v1/consortium/:consortiumId', (req: Request, res: Response, next: NextFunction) => {
150-
// res.json({ swagger: 'TODO' });
151-
// });
155+
await Promise.all(webServicesInstalled);
156+
this.log.info(`Installed ${webServicesInstalled.length} web services OK`);
152157

153158
const apiPort: number = this.options.config.get("apiPort");
154159
const apiHost: string = this.options.config.get("apiHost");

packages/bif-cmd-api-server/src/main/typescript/config/config-service.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { randomBytes } from "crypto";
22
import convict, { Schema, Config, SchemaObj } from "convict";
33
import secp256k1 from "secp256k1";
44
import { v4 as uuidV4 } from "uuid";
5-
import { LoggerProvider, Logger } from "@hyperledger-labs/bif-common";
5+
import {
6+
LoggerProvider,
7+
Logger,
8+
LogLevelDesc,
9+
} from "@hyperledger-labs/bif-common";
610

711
export interface IBifApiServerOptions {
812
configFile: string;
913
bifNodeId: string;
10-
logLevel: string;
14+
logLevel: LogLevelDesc;
1115
cockpitHost: string;
1216
cockpitPort: number;
1317
cockpitWwwRoot: string;

packages/bif-cmd-api-server/src/main/typescript/consortium/model/bif-node.ts

-4
This file was deleted.

packages/bif-cmd-api-server/src/main/typescript/consortium/model/consortium-wrapper.ts

-4
This file was deleted.

packages/bif-cmd-api-server/src/main/typescript/consortium/model/consortium.ts

-8
This file was deleted.

packages/bif-cmd-api-server/src/main/typescript/consortium/routes/create-consortium-endpoint-v1.ts

-70
This file was deleted.

packages/bif-cmd-api-server/src/main/typescript/consortium/routes/sign-data-endpoint-v1.ts

-32
This file was deleted.

0 commit comments

Comments
 (0)