Skip to content

Commit

Permalink
squash! - checkpoint 3 - both tests work
Browse files Browse the repository at this point in the history
TODO:
1. Figure out how to stop the generator from mangling the model
property names into capital case (getCreatedat)
2. Figure out how to export the OpenAPI model classes and the
gRPC model classes at the same time without name conflicts
(they have the same names)
3. Implement streaming healthcheck endpiont with gRPC
4. Allow plugins to hook in their own gRPC service implementations.

Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed Aug 9, 2021
1 parent 1ab96ef commit bfaab94
Show file tree
Hide file tree
Showing 35 changed files with 318 additions and 1,416 deletions.
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"git-cz": "4.7.6",
"globby": "10.0.2",
"grpc-tools": "1.11.2",
"grpc-web": "1.2.1",
"grpc_tools_node_protoc_ts": "5.3.1",
"husky": "4.2.5",
"inquirer": "8.1.1",
"json5": "2.2.0",
Expand All @@ -115,8 +115,6 @@
"npm-run-all": "4.1.5",
"npm-watch": "0.7.0",
"prettier": "2.0.5",
"protobufjs": "6.11.2",
"protoc-gen-grpc-web": "1.2.1",
"run-time-error": "1.4.0",
"secp256k1": "4.0.0",
"shebang-loader": "0.0.1",
Expand All @@ -127,8 +125,6 @@
"tape-promise": "4.0.0",
"ts-loader": "6.2.1",
"ts-node": "10.1.0",
"ts-proto": "1.82.5",
"ts-protoc-gen": "0.15.0",
"typescript": "4.2.4",
"webpack": "5.36.2",
"webpack-bundle-analyzer": "4.4.2",
Expand Down
12 changes: 2 additions & 10 deletions packages/cactus-cmd-api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@
"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:openapi": "openapi-generator-cli generate -i ./src/main/json/openapi.json -g protobuf-schema --additional-properties=packageName=org.hyperledger.cactus.cmd_api_server -o ./src/main/proto/generated/openapi/",
"old-proto:pbjs:openapi": "yarn pbjs --es6 --target static --wrap=es6 --out ./src/main/typescript/generated/proto/pbjs.openapi.js ./src/main/proto/generated/openapi/models/watch_healthcheck_v1.proto ./src/main/proto/generated/openapi/models/memory_usage.proto ./src/main/proto/generated/openapi/models/health_check_response.proto ./src/main/proto/generated/openapi/services/default_service.proto",
"old-proto:pbts:openapi": "yarn pbts --out ./src/main/typescript/generated/proto/pbjs.openapi.d.ts ./src/main/typescript/generated/proto/pbjs.openapi.js",
"old-proto:grpc-web:openapi": "yarn grpc_tools_node_protoc --js_out=import_style=commonjs:./src/main/typescript/generated/proto/grpc-web/ --grpc_out=generate_package_definition:./src/main/typescript/generated/proto/grpc-web/ ./src/main/proto/generated/openapi/models/watch_healthcheck_v1.proto ./src/main/proto/generated/openapi/models/memory_usage.proto ./src/main/proto/generated/openapi/models/health_check_response.proto ./src/main/proto/generated/openapi/services/default_service.proto --proto_path ./src/main/proto/generated/openapi/",
"proto:proto-ts-grpc-web:openapi": "yarn grpc_tools_node_protoc --plugin=../../node_modules/ts-proto/protoc-gen-ts_proto --plugin=../../node_modules/ts-protoc-gen/bin/protoc-gen-ts --js_out=import_style=commonjs,binary:./src/main/typescript/generated/proto/grpc-web/ --ts_out=service=grpc-node,mode=grpc-js:./src/main/typescript/generated/proto/grpc-web/ --grpc_out=generate_package_definition:./src/main/typescript/generated/proto/grpc-web/ --ts_proto_out=./src/main/typescript/generated/proto/ts-proto/ --ts_proto_opt=context=false --ts_proto_opt=esModuleInterop=true --ts_proto_opt=env=both --ts_proto_opt=lowerCaseServiceMethods=true --ts_proto_opt=outputClientImpl=grpc-web --ts_proto_opt=outputJsonMethods=true --ts_proto_opt=returnObservable=true --ts_proto_opt=addGrpcMetadata=true ./src/main/proto/generated/openapi/models/watch_healthcheck_v1.proto ./src/main/proto/generated/openapi/models/memory_usage.proto ./src/main/proto/generated/openapi/models/health_check_response.proto ./src/main/proto/generated/openapi/services/default_service.proto --proto_path ./src/main/proto/generated/openapi/",
"old-proto:pbjs": "yarn pbjs --es6 --target static --wrap=es6 --out ./src/main/typescript/generated/proto/pbjs.js ./src/main/proto/healthcheck.proto",
"old-proto:pbts": "yarn pbts --out ./src/main/typescript/generated/proto/pbts.d.ts ./src/main/typescript/generated/proto/pbjs.js",
"old-proto:protoc": "yarn grpc_tools_node_protoc ./src/main/proto/healthcheck.proto",
"codegen:proto": "run-s proto:openapi proto:proto-ts-grpc-web:openapi",
"proto:protoc-gen-ts": "yarn run grpc_tools_node_protoc --plugin=protoc-gen-ts=../../node_modules/.bin/protoc-gen-ts --js_out=import_style=commonjs,binary:./src/main/typescript/generated/proto/protoc-gen-ts/ --ts_out=grpc_js:./src/main/typescript/generated/proto/protoc-gen-ts/ --grpc_out=grpc_js:./src/main/typescript/generated/proto/protoc-gen-ts/ --proto_path ./src/main/proto/generated/openapi/ --proto_path ./src/main/proto/generated/openapi/models/ --proto_path ./src/main/proto/generated/openapi/services/ ./src/main/proto/generated/openapi/models/*.proto ./src/main/proto/generated/openapi/services/*.proto",
"codegen:proto": "run-s proto:openapi proto:protoc-gen-ts",
"codegen": "run-p 'codegen:*'",
"watch": "npm-watch",
"webpack": "npm-run-all webpack:dev webpack:prod",
Expand Down Expand Up @@ -86,8 +80,6 @@
"@hyperledger/cactus-common": "0.6.0",
"@hyperledger/cactus-core": "0.6.0",
"@hyperledger/cactus-core-api": "0.6.0",
"@improbable-eng/grpc-web": "0.14.0",
"@improbable-eng/grpc-web-node-http-transport": "0.14.0",
"@thream/socketio-jwt": "2.1.0",
"axios": "0.21.1",
"body-parser": "1.19.0",
Expand Down
45 changes: 7 additions & 38 deletions packages/cactus-cmd-api-server/src/main/typescript/api-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { RuntimeError } from "run-time-error";
import { gte } from "semver";
import npm from "npm";
import expressHttpProxy from "express-http-proxy";
import { Server as GrpcServer, ServerUnaryCall } from "@grpc/grpc-js";
import { Server as GrpcServer } from "@grpc/grpc-js";
import type { Application, Request, Response, RequestHandler } from "express";
import express from "express";
import { OpenApiValidator } from "express-openapi-validator";
Expand Down Expand Up @@ -43,9 +43,8 @@ import { PrometheusExporter } from "./prometheus-exporter/prometheus-exporter";
import { AuthorizerFactory } from "./authzn/authorizer-factory";
import { WatchHealthcheckV1 } from "./generated/openapi/typescript-axios";
import { WatchHealthcheckV1Endpoint } from "./web-services/watch-healthcheck-v1-endpoint";
import { HealthCheckResponsePb, MemoryUsagePb } from "./public-api";
import { Empty } from "./generated/proto/ts-proto/google/protobuf/empty";
// import { DefaultServiceService } from "./generated/proto/grpc-web/services/default_service_grpc_pb";
import { DefaultServiceService } from "./generated/proto/protoc-gen-ts/services/default_service_grpc_pb";
import { GrpcServerApiServer } from "./web-services/grpc/grpc-server-api-server";

export interface IApiServerConstructorOptions {
readonly pluginRegistry?: PluginRegistry;
Expand Down Expand Up @@ -539,40 +538,10 @@ export class ApiServer {
reject(new RuntimeError("Start the gRPC server failed: ", error));
return;
}
const modulePath =
"./generated/proto/grpc-web/services/default_service_grpc_pb";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const default_service_grpc_pb = require(/* webpackIgnore: true */ modulePath);

const svc =
default_service_grpc_pb[
"org.hyperledger.cactus.cmd_api_server.DefaultService"
];

this.grpcServer.addService(svc, {
getHealthCheckV1: (
call: ServerUnaryCall<Empty, HealthCheckResponsePb>,
callback: (
err: Error | null,
value: HealthCheckResponsePb,
) => void,
) => {
this.log.debug("gRPC: GetHealthCheckV1 peer:", call.getPeer());
const x = new HealthCheckResponsePb();
x.setCreatedat(new Date().toJSON());
x.setSuccess(true);
const memoryUsage = new MemoryUsagePb();
const mu = process.memoryUsage();
memoryUsage.setHeaptotal(mu.heapTotal);
memoryUsage.setHeapused(mu.heapUsed);
memoryUsage.setRss(mu.rss);
memoryUsage.setExternal(mu.external);
memoryUsage.setArraybuffers(mu.arrayBuffers);
x.setMemoryusage(memoryUsage);
callback(null, x);
},
});
this.grpcServer.addService(
DefaultServiceService,
new GrpcServerApiServer(),
);
this.grpcServer.start();
// FIXME - ensure family is set correctly to v4 or v6 depending on
// the host address' family (there must be some library to parse it)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// package: org.hyperledger.cactus.cmd_api_server
// file: models/health_check_response.proto

/* tslint:disable */
/* eslint-disable */

import * as jspb from "google-protobuf";
import * as models_memory_usage_pb from "../models/memory_usage_pb";

export class HealthCheckResponse extends jspb.Message {
getSuccess(): boolean;
setSuccess(value: boolean): HealthCheckResponse;
getCreatedat(): string;
setCreatedat(value: string): HealthCheckResponse;

hasMemoryusage(): boolean;
clearMemoryusage(): void;
getMemoryusage(): models_memory_usage_pb.MemoryUsage | undefined;
setMemoryusage(value?: models_memory_usage_pb.MemoryUsage): HealthCheckResponse;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): HealthCheckResponse.AsObject;
static toObject(includeInstance: boolean, msg: HealthCheckResponse): HealthCheckResponse.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: HealthCheckResponse, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): HealthCheckResponse;
static deserializeBinaryFromReader(message: HealthCheckResponse, reader: jspb.BinaryReader): HealthCheckResponse;
}

export namespace HealthCheckResponse {
export type AsObject = {
success: boolean,
createdat: string,
memoryusage?: models_memory_usage_pb.MemoryUsage.AsObject,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// package: org.hyperledger.cactus.cmd_api_server
// file: models/memory_usage.proto

/* tslint:disable */
/* eslint-disable */

import * as jspb from "google-protobuf";

export class MemoryUsage extends jspb.Message {
getRss(): number;
setRss(value: number): MemoryUsage;
getHeaptotal(): number;
setHeaptotal(value: number): MemoryUsage;
getHeapused(): number;
setHeapused(value: number): MemoryUsage;
getExternal(): number;
setExternal(value: number): MemoryUsage;
getArraybuffers(): number;
setArraybuffers(value: number): MemoryUsage;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): MemoryUsage.AsObject;
static toObject(includeInstance: boolean, msg: MemoryUsage): MemoryUsage.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: MemoryUsage, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): MemoryUsage;
static deserializeBinaryFromReader(message: MemoryUsage, reader: jspb.BinaryReader): MemoryUsage;
}

export namespace MemoryUsage {
export type AsObject = {
rss: number,
heaptotal: number,
heapused: number,
external: number,
arraybuffers: number,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// package: org.hyperledger.cactus.cmd_api_server
// file: models/watch_healthcheck_v1.proto

/* tslint:disable */
/* eslint-disable */

import * as jspb from "google-protobuf";

export class WatchHealthcheckV1 extends jspb.Message {

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): WatchHealthcheckV1.AsObject;
static toObject(includeInstance: boolean, msg: WatchHealthcheckV1): WatchHealthcheckV1.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: WatchHealthcheckV1, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): WatchHealthcheckV1;
static deserializeBinaryFromReader(message: WatchHealthcheckV1, reader: jspb.BinaryReader): WatchHealthcheckV1;
}

export namespace WatchHealthcheckV1 {
export type AsObject = {
}
}
Loading

0 comments on commit bfaab94

Please sign in to comment.