Skip to content

Commit

Permalink
feat(cmd-api-server): support grpc web services hyperledger-cacti#1189
Browse files Browse the repository at this point in the history
Primary change:
--------------
The API server now contains a gRPC server in addition to what it
had before (HTTP+SocketIO servers) so that through this it can
provide the opportunity for plugins to expose their gRPC services
via the Cactus API server. It is possible for plugins to serve
their requests on multiple protocols at the same time which means
that this is a big step towards our goal of being properly multi-
protocol capable.

Secondary change(s):
-------------------
1. The "allowJs" flag for the Typescript compiler is now specified
as true for the cmd-api-server package which is necessary because
without this the .js files generated by protoc do not get copied
over from the ./src/main/typescript/generated/ folder to the
./dist/lib/.../generated folder with the rest of the build files.
It is not ideal that we are generating JS code into the TS folder
but it does come with .d.ts files and this is the "state of the
art" at the moment becaues we can only do what the protocol
buffer compiler will support and this is it.

Later on we should improve on this situation once the tooling
catches up and adds support to generate straight up TS code instead
of JS+d.ts files so that we keep to our convention of having only
TS code under ./src/main/typescript for obvious reasons.

TODO:
----

1. Figure out how to stop the generator from mangling the model
property names into capital case (getCreatedat)
See: https://github.com/protocolbuffers/protobuf/issues/8608

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.

5. Verify with test case that the secure credentials flavor also
works.

Fixes hyperledger-cacti#1189

Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed Aug 11, 2021
1 parent 0e4c7c5 commit 3df47d2
Show file tree
Hide file tree
Showing 58 changed files with 1,718 additions and 47 deletions.
11 changes: 9 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"gopath",
"grpc",
"grpcs",
"grpcwebtext",
"hashicorp",
"Healthcheck",
"HTLC",
Expand Down Expand Up @@ -68,10 +69,17 @@
"NODETXPOOLACK",
"notok",
"Oidc",
"oneofs",
"onsi",
"OpenAPI",
"openethereum",
"organisation",
"parameterizable",
"pbjs",
"pbts",
"proto",
"protobuf",
"protoc",
"protos",
"RUSTC",
"Secp",
Expand All @@ -91,8 +99,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/**
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export class CarbonAccountingApp {
config.apiHost = addressInfoApi.address;
config.cockpitHost = addressInfoCockpit.address;
config.cockpitPort = addressInfoCockpit.port;
config.grpcPort = 0; // TODO - make this configurable as well
config.logLevel = this.options.logLevel || "INFO";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ test(testCase, async (t: Test) => {
apiSrvOpts.apiCorsDomainCsv = "*";
apiSrvOpts.apiPort = 0;
apiSrvOpts.cockpitPort = 0;
apiSrvOpts.grpcPort = 0;
apiSrvOpts.apiTlsEnabled = false;
apiSrvOpts.plugins = [];
const convictConfig = configService.newExampleConfigConvict(apiSrvOpts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@angular/compiler-cli": "12.1.1",
"@angular/language-service": "12.1.1",
"@ionic/angular-toolkit": "2.3.0",
"@types/jasminewd2": "2.0.3",
"@types/node": "12.11.1",
"codelyzer": "6.0.2",
"jasmine-core": "3.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ export class SupplyChainApp {
properties.apiHost = addressInfoApi.address;
properties.cockpitHost = addressInfoCockpit.address;
properties.cockpitPort = addressInfoCockpit.port;
properties.grpcPort = 0; // TODO - make this configurable as well
properties.logLevel = this.options.logLevel || "INFO";

const apiServer = new ApiServer({
Expand Down
1 change: 0 additions & 1 deletion examples/cactus-example-supply-chain-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@angular/compiler-cli": "12.1.1",
"@angular/language-service": "12.1.1",
"@ionic/angular-toolkit": "2.3.0",
"@types/jasminewd2": "2.0.3",
"@types/node": "12.11.1",
"codelyzer": "6.0.2",
"jasmine-core": "3.6.0",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"@commitlint/config-conventional": "8.0.0",
"@openapitools/openapi-generator-cli": "2.3.3",
"@types/fs-extra": "9.0.11",
"@types/jasminewd2": "2.0.10",
"@types/node-fetch": "2.5.4",
"@types/tape": "4.13.0",
"@types/tape-promise": "4.0.1",
Expand All @@ -100,6 +99,8 @@
"fs-extra": "10.0.0",
"git-cz": "4.7.6",
"globby": "10.0.2",
"grpc-tools": "1.11.2",
"grpc_tools_node_protoc_ts": "5.3.1",
"husky": "4.2.5",
"inquirer": "8.1.1",
"json5": "2.2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/cactus-cmd-api-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const main = async () => {
apiServerOptions.apiCorsDomainCsv = "your.domain.example.com";
apiServerOptions.apiPort = 3000;
apiServerOptions.cockpitPort = 3100;
apiServerOptions.grpcPort = 5000;
// Disble TLS (or provide TLS certs for secure HTTP if you are deploying to production)
apiServerOptions.apiTlsEnabled = false;
apiServerOptions.plugins = [
Expand Down
8 changes: 7 additions & 1 deletion packages/cactus-cmd-api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
"browser": "dist/cactus-cmd-api-server.web.umd.js",
"browserMinified": "dist/cactus-cmd-api-server.web.umd.min.js",
"module": "dist/lib/main/typescript/index.js",
"types": "dist/types/main/typescript/index.d.ts",
"types": "dist/lib/main/typescript/index.d.ts",
"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: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/",
"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 @@ -72,6 +75,8 @@
},
"homepage": "https://github.com/hyperledger/cactus#readme",
"dependencies": {
"@grpc/grpc-js": "1.3.6",
"@grpc/proto-loader": "0.6.4",
"@hyperledger/cactus-common": "0.7.0",
"@hyperledger/cactus-core": "0.7.0",
"@hyperledger/cactus-core-api": "0.7.0",
Expand Down Expand Up @@ -108,6 +113,7 @@
"@types/express": "4.17.8",
"@types/express-http-proxy": "1.6.1",
"@types/express-jwt": "6.0.1",
"@types/google-protobuf": "3.15.3",
"@types/jsonwebtoken": "8.5.1",
"@types/multer": "1.4.5",
"@types/node-forge": "0.9.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
README.md
models/health_check_response.proto
models/memory_usage.proto
models/watch_healthcheck_v1.proto
services/default_service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.1.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# gPRC for org.hyperledger.cactus.cmd_api_server

Interact with a Cactus deployment through HTTP.

## Overview
These files were generated by the [OpenAPI Generator](https://openapi-generator.tech) project.

- API version: 0.0.1
- Package version:
- Build package: org.openapitools.codegen.languages.ProtobufSchemaCodegen

## Usage

Below are some usage examples for Go and Ruby. For other languages, please refer to https://grpc.io/docs/quickstart/.

### Go
```
# assuming `protoc-gen-go` has been installed with `go get -u github.com/golang/protobuf/protoc-gen-go`
mkdir /var/tmp/go/
protoc --go_out=/var/tmp/go/ services/*
protoc --go_out=/var/tmp/go/ models/*
```

### Ruby
```
# assuming `grpc_tools_ruby_protoc` has been installed via `gem install grpc-tools`
RUBY_OUTPUT_DIR="/var/tmp/ruby/org.hyperledger.cactus.cmd_api_server"
mkdir $RUBY_OUTPUT_DIR
grpc_tools_ruby_protoc --ruby_out=$RUBY_OUTPUT_DIR --grpc_out=$RUBY_OUTPUT_DIR/lib services/*
grpc_tools_ruby_protoc --ruby_out=$RUBY_OUTPUT_DIR --grpc_out=$RUBY_OUTPUT_DIR/lib models/*
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Hyperledger Cactus API
Interact with a Cactus deployment through HTTP.
The version of the OpenAPI document: 0.0.1
Generated by OpenAPI Generator: https://openapi-generator.tech
*/

syntax = "proto3";

package org.hyperledger.cactus.cmd_api_server;

import public "models/memory_usage.proto";

message HealthCheckResponse {

bool success = 256557056;

string createdAt = 61500732;

MemoryUsage memoryUsage = 335792418;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Hyperledger Cactus API
Interact with a Cactus deployment through HTTP.
The version of the OpenAPI document: 0.0.1
Generated by OpenAPI Generator: https://openapi-generator.tech
*/

syntax = "proto3";

package org.hyperledger.cactus.cmd_api_server;


message MemoryUsage {

float rss = 113234;

float heapTotal = 114487480;

float heapUsed = 30910521;

float external = 210148408;

float arrayBuffers = 116952168;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Hyperledger Cactus API
Interact with a Cactus deployment through HTTP.
The version of the OpenAPI document: 0.0.1
Generated by OpenAPI Generator: https://openapi-generator.tech
*/

syntax = "proto3";

package org.hyperledger.cactus.cmd_api_server;


message WatchHealthcheckV1 {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Hyperledger Cactus API
Interact with a Cactus deployment through HTTP.
The version of the OpenAPI document: 0.0.1
Generated by OpenAPI Generator: https://openapi-generator.tech
*/

syntax = "proto3";

package org.hyperledger.cactus.cmd_api_server;

import "google/protobuf/empty.proto";
import public "models/health_check_response.proto";

service DefaultService {
rpc GetHealthCheckV1 (google.protobuf.Empty) returns (HealthCheckResponse);

rpc GetPrometheusMetricsV1 (google.protobuf.Empty) returns (GetPrometheusMetricsV1Response);

}

message GetPrometheusMetricsV1Response {
string data = 1;
}

Loading

0 comments on commit 3df47d2

Please sign in to comment.