Skip to content

Commit

Permalink
feat(cmd-api-server): support grpc web services hyperledger#1189
Browse files Browse the repository at this point in the history
WORK IN PROGRESS!

Fixes hyperledger#1189

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Aug 8, 2021
1 parent b0688e8 commit 9911891
Show file tree
Hide file tree
Showing 10 changed files with 1,378 additions and 4 deletions.
8 changes: 6 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
"OpenAPI",
"openethereum",
"organisation",
"parameterizable",
"pbjs",
"pbts",
"proto",
"protoc",
"protos",
"RUSTC",
"Secp",
Expand All @@ -91,8 +96,7 @@
"uuidv",
"vscc",
"wasm",
"Xdai",
"parameterizable"
"Xdai"
],
"dictionaries": [
"typescript,node,npm,go,rust"
Expand Down
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
# **/coverage/**

# typings/**

**/src/main/typescript/generated/proto/**
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"fs-extra": "10.0.0",
"git-cz": "4.7.6",
"globby": "10.0.2",
"grpc-tools": "1.11.2",
"husky": "4.2.5",
"inquirer": "8.1.1",
"json5": "2.2.0",
Expand All @@ -114,6 +115,7 @@
"npm-run-all": "4.1.5",
"npm-watch": "0.7.0",
"prettier": "2.0.5",
"protobufjs": "6.11.2",
"run-time-error": "1.4.0",
"secp256k1": "4.0.0",
"shebang-loader": "0.0.1",
Expand All @@ -124,6 +126,7 @@
"tape-promise": "4.0.0",
"ts-loader": "6.2.1",
"ts-node": "10.1.0",
"ts-protoc-gen": "0.15.0",
"typescript": "4.2.4",
"webpack": "5.36.2",
"webpack-bundle-analyzer": "4.4.2",
Expand Down
4 changes: 4 additions & 0 deletions packages/cactus-cmd-api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"scripts": {
"generate-sdk": "openapi-generator-cli generate -i ./src/main/json/openapi.json -g typescript-axios -o ./src/main/typescript/generated/openapi/typescript-axios/ --reserved-words-mappings protected=protected",
"codegen:openapi": "npm run generate-sdk",
"proto:pbjs": "yarn pbjs --es6 --target static --wrap=es6 --out ./src/main/typescript/generated/proto/pbjs.js ./src/main/proto/healthcheck.proto",
"proto:pbts": "yarn pbts --out ./src/main/typescript/generated/proto/pbts.d.ts ./src/main/typescript/generated/proto/pbjs.js",
"proto:protoc": "yarn grpc_tools_node_protoc ./src/main/proto/healthcheck.proto",
"codegen:proto": "run-s 'protoc:*'",
"codegen": "run-p 'codegen:*'",
"watch": "npm-watch",
"webpack": "npm-run-all webpack:dev webpack:prod",
Expand Down
25 changes: 25 additions & 0 deletions packages/cactus-cmd-api-server/src/main/proto/healthcheck.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";

package org.hyperledger.cactus.cmd_api_server;

service Healthcheck {
// Obtains the memory usage statistics of the API server's NodeJS process.
rpc GetHealtcheck(HealthcheckRequest) returns (stream HealthcheckResponse) {}
}

message MemoryUsage {
int64 rss = 1;
int64 heapTotal = 2;
int64 heapUsed = 3;
int64 external = 4;
int64 arrayBuffers = 5;
}

message HealthcheckRequest {
}

message HealthcheckResponse {
bool success = 1;
string createdAt = 2;
MemoryUsage memoryUsage = 3;
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export {
} from "./authzn/authorizer-factory";
export { IAuthorizationConfig } from "./authzn/i-authorization-config";
export { AuthorizationProtocol } from "./config/authorization-protocol";

export * from "./generated/proto/pbts";
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Socket as SocketIoSocket } from "socket.io";

import { Logger, Checks } from "@hyperledger/cactus-common";
import { LogLevelDesc, LoggerProvider } from "@hyperledger/cactus-common";

import { org } from "../../generated/proto/pbts";
import { WatchHealthcheckV1 } from "../../generated/openapi/typescript-axios/api";

const { HealthcheckResponse } = org.hyperledger.cactus.cmd_api_server;

export interface IWatchHealthcheckV1EndpointOptions {
logLevel?: LogLevelDesc;
socket: SocketIoSocket;
process: NodeJS.Process;
}

export class WatchHealthcheckV1Endpoint {
public static readonly CLASS_NAME = "WatchHealthcheckV1Endpoint";

private readonly log: Logger;
private readonly process: NodeJS.Process;

public get className(): string {
return WatchHealthcheckV1Endpoint.CLASS_NAME;
}

constructor(public readonly options: IWatchHealthcheckV1EndpointOptions) {
const fnTag = `${this.className}#constructor()`;
Checks.truthy(options, `${fnTag} arg options`);
Checks.truthy(options.process, `${fnTag} arg options.process`);

this.process = options.process;

const level = this.options.logLevel || "INFO";
const label = this.className;
this.log = LoggerProvider.getOrCreate({ level, label });
}

public async subscribe(): Promise<void> {
const { log } = this;
log.debug(`${WatchHealthcheckV1.Subscribe} => FIXME - connection ID here`);

const timerId = setInterval(() => {
try {
const next: org.hyperledger.cactus.cmd_api_server.IHealthcheckResponse = HealthcheckResponse.create(
{
createdAt: new Date().toJSON(),
memoryUsage: this.process.memoryUsage(),
success: true,
},
);

const validationMessage = HealthcheckResponse.verify(next);
if (validationMessage) {
// socket.emit(WatchHealthcheckV1.Error, validationMessage);
} else {
// socket.emit(WatchHealthcheckV1.Next, next);
}
} catch (ex) {
log.error(`Failed to construct health check response:`, ex);
// socket.emit(WatchHealthcheckV1.Error, ex);
clearInterval(timerId);
}
}, 1000);

// socket.on("disconnect", async (reason: string) => {
// log.debug("WebSocket:disconnect reason=%o", reason);
// clearInterval(timerId);
// });

// socket.on(WatchHealthcheckV1.Unsubscribe, () => {
// clearInterval(timerId);
// });
}
}
38 changes: 36 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,21 @@
npmlog "^4.1.2"
write-file-atomic "^3.0.3"

"@mapbox/node-pre-gyp@^1.0.5":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.5.tgz#2a0b32fcb416fb3f2250fd24cb2a81421a4f5950"
integrity sha512-4srsKPXWlIxp5Vbqz5uLfBN+du2fJChBoYn/f2h991WLdk7jUvcSk/McVLSv/X+xQIPI8eGD5GjrnygdyHnhPA==
dependencies:
detect-libc "^1.0.3"
https-proxy-agent "^5.0.0"
make-dir "^3.1.0"
node-fetch "^2.6.1"
nopt "^5.0.0"
npmlog "^4.1.2"
rimraf "^3.0.2"
semver "^7.3.4"
tar "^6.1.0"

"@multiformats/base-x@^4.0.1":
version "4.0.1"
resolved "https://registry.npmjs.org/@multiformats/base-x/-/base-x-4.0.1.tgz"
Expand Down Expand Up @@ -10151,6 +10166,11 @@ google-p12-pem@^3.0.3:
dependencies:
node-forge "^0.10.0"

google-protobuf@^3.15.5:
version "3.17.3"
resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.17.3.tgz#f87595073545a77946c8f0b67c302c5f7646d700"
integrity sha512-OVPzcSWIAJ+d5yiHyeaLrdufQtrvaBrF4JQg+z8ynTkbO3uFcujqXszTumqg1cGsAsjkWnI+M5B1xZ19yR4Wyg==

got@9.6.0, got@^9.6.0:
version "9.6.0"
resolved "https://registry.npmjs.org/got/-/got-9.6.0.tgz"
Expand Down Expand Up @@ -10237,6 +10257,13 @@ growl@1.10.5:
resolved "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz"
integrity sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==

grpc-tools@1.11.2:
version "1.11.2"
resolved "https://registry.yarnpkg.com/grpc-tools/-/grpc-tools-1.11.2.tgz#22d802d40012510ccc6591d11f9c94109ac07aab"
integrity sha512-4+EgpnnkJraamY++oyBCw5Hp9huRYfgakjNVKbiE3PgO9Tv5ydVlRo7ZyGJ0C0SEiA7HhbVc1sNNtIyK7FiEtg==
dependencies:
"@mapbox/node-pre-gyp" "^1.0.5"

gtoken@^5.0.4:
version "5.3.0"
resolved "https://registry.yarnpkg.com/gtoken/-/gtoken-5.3.0.tgz#6536eb2880d9829f0b9d78f756795d4d9064b217"
Expand Down Expand Up @@ -16584,9 +16611,9 @@ proto-list@~1.2.1:
resolved "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz"
integrity sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=

protobufjs@^6.10.0, protobufjs@^6.10.2, protobufjs@^6.11.2:
protobufjs@6.11.2, protobufjs@^6.10.0, protobufjs@^6.10.2, protobufjs@^6.11.2:
version "6.11.2"
resolved "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.2.tgz"
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.11.2.tgz#de39fabd4ed32beaa08e9bb1e30d08544c1edf8b"
integrity sha512-4BQJoPooKJl2G9j3XftkIXjoC9C0Av2NOrWmbLWT1vH32GcSUHjM0Arra6UfTsVyfMAuFzaLucXn1sadxJydAw==
dependencies:
"@protobufjs/aspromise" "^1.1.2"
Expand Down Expand Up @@ -19624,6 +19651,13 @@ ts-node@8.3.0:
source-map-support "^0.5.6"
yn "^3.0.0"

ts-protoc-gen@0.15.0:
version "0.15.0"
resolved "https://registry.yarnpkg.com/ts-protoc-gen/-/ts-protoc-gen-0.15.0.tgz#2fec5930b46def7dcc9fa73c060d770b7b076b7b"
integrity sha512-TycnzEyrdVDlATJ3bWFTtra3SCiEP0W0vySXReAuEygXCUr1j2uaVyL0DhzjwuUdQoW5oXPwk6oZWeA0955V+g==
dependencies:
google-protobuf "^3.15.5"

tsconfig-paths@^3.9.0:
version "3.10.1"
resolved "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.10.1.tgz"
Expand Down

0 comments on commit 9911891

Please sign in to comment.