Skip to content

Commit

Permalink
feat(plugin-web-service-consortium): add dedicated plugin for consort…
Browse files Browse the repository at this point in the history
…ium 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 <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed May 19, 2020
1 parent bb18774 commit f63f5a5
Show file tree
Hide file tree
Showing 45 changed files with 2,400 additions and 375 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
"build:frontend": "lerna exec --stream --scope '*/*cockpit' -- ng build --prod",
"build:backend": "npm-run-all lint clean generate-sdk tsc webpack",
"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'",
"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'",
"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'",
"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'",
"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'",
"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'",
"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'",
"webpack": "npm-run-all webpack:web:dev webpack:node:dev webpack:web:prod webpack:node:prod",
"webpack:web:prod": "lerna exec --stream --ignore '*/*{cockpit,server,test-tooling}' -- webpack --env=prod --target=web --config ../../webpack.config.js",
Expand Down
26 changes: 17 additions & 9 deletions packages/bif-cmd-api-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/bif-cmd-api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@types/compression": "1.7.0",
"@types/convict": "5.2.0",
"@types/cors": "2.8.6",
"@types/express": "4.17.3",
"@types/express": "4.17.6",
"@types/joi": "14.3.4",
"@types/multer": "1.4.2",
"@types/secp256k1": "3.5.3",
Expand Down
55 changes: 30 additions & 25 deletions packages/bif-cmd-api-server/src/main/typescript/api-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import express, {
Express,
Request,
Response,
NextFunction,
RequestHandler,
Application,
} from "express";
Expand All @@ -21,8 +20,7 @@ import {
isIPluginWebService,
IPluginWebService,
} from "@hyperledger-labs/bif-core-api";
import { CreateConsortiumEndpointV1 } from "./consortium/routes/create-consortium-endpoint-v1";
import { IBifApiServerOptions, ConfigService } from "./config/config-service";
import { IBifApiServerOptions } from "./config/config-service";
import { BIF_OPEN_API_JSON } from "./openapi-spec";
import { Logger, LoggerProvider } from "@hyperledger-labs/bif-common";
import { Servers } from "./common/servers";
Expand Down Expand Up @@ -70,6 +68,21 @@ export class ApiServer {
}

public async shutdown(): Promise<void> {
this.log.info(`Shutting down API server ...`);
const webServicesShutdown = this.options.plugins
.filter((pluginInstance) => isIPluginWebService(pluginInstance))
.map((pluginInstance: ICactusPlugin) => {
return (pluginInstance as IPluginWebService).shutdown();
});

this.log.info(
`Found ${webServicesShutdown.length} web service plugin(s), shutting them down ...`
);
await Promise.all(webServicesShutdown);
this.log.info(
`Shut down ${webServicesShutdown.length} web service plugin(s) OK`
);

if (this.httpServerApi) {
this.log.info(`Closing HTTP server of the API...`);
await Servers.shutdown(this.httpServerApi);
Expand Down Expand Up @@ -124,31 +137,23 @@ export class ApiServer {
const openApiValidator = this.createOpenApiValidator();
await openApiValidator.install(app);

app.get(
"/healthcheck",
(req: Request, res: Response, next: NextFunction) => {
res.json({ success: true, timestamp: new Date() });
}
);

const storage: IPluginKVStorage = await this.createStoragePlugin();
const configService = new ConfigService();
const config = configService.getOrCreate();
{
const endpoint = new CreateConsortiumEndpointV1({ storage, config });
app.post(endpoint.getPath(), endpoint.handleRequest.bind(endpoint));
}
const healthcheckHandler = (req: Request, res: Response) => {
res.json({
success: true,
createdAt: new Date(),
memoryUsage: process.memoryUsage(),
});
};
app.get("/api/v1/api-server/healthcheck", healthcheckHandler);

this.options.plugins
this.log.info(`Starting to install web services...`);
const webServicesInstalled = this.options.plugins
.filter((pluginInstance) => isIPluginWebService(pluginInstance))
.forEach((pluginInstance: ICactusPlugin) => {
(pluginInstance as IPluginWebService).installWebService(app);
.map((pluginInstance: ICactusPlugin) => {
return (pluginInstance as IPluginWebService).installWebServices(app);
});

// FIXME
// app.get('/api/v1/consortium/:consortiumId', (req: Request, res: Response, next: NextFunction) => {
// res.json({ swagger: 'TODO' });
// });
await Promise.all(webServicesInstalled);
this.log.info(`Installed ${webServicesInstalled.length} web services OK`);

const apiPort: number = this.options.config.get("apiPort");
const apiHost: string = this.options.config.get("apiHost");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { randomBytes } from "crypto";
import convict, { Schema, Config, SchemaObj } from "convict";
import secp256k1 from "secp256k1";
import { v4 as uuidV4 } from "uuid";
import { LoggerProvider, Logger } from "@hyperledger-labs/bif-common";
import {
LoggerProvider,
Logger,
LogLevelDesc,
} from "@hyperledger-labs/bif-common";

export interface IBifApiServerOptions {
configFile: string;
bifNodeId: string;
logLevel: string;
logLevel: LogLevelDesc;
cockpitHost: string;
cockpitPort: number;
cockpitWwwRoot: string;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit f63f5a5

Please sign in to comment.