Skip to content

Commit 21f949f

Browse files
petermetzhanxu12
authored andcommitted
feat(cmd-api-server): support grpc web services hyperledger-cacti#1189
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. Custom protobuf-schema openapi generator template added because of this issue: thesayyn/protoc-gen-ts#82 (we override the stock template to not use the public keyword) TODO: ---- 1. Implement streaming healthcheck endpiont with gRPC 2. Allow plugins to hook in their own gRPC service implementations. Fixes hyperledger-cacti#1189 Signed-off-by: Peter Somogyvari <[email protected]>
1 parent 804e882 commit 21f949f

File tree

54 files changed

+1225
-39
lines changed

Some content is hidden

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

54 files changed

+1225
-39
lines changed

.cspell.json

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"gopath",
3737
"grpc",
3838
"grpcs",
39+
"grpcwebtext",
3940
"hashicorp",
4041
"Healthcheck",
4142
"HTLC",
@@ -72,12 +73,16 @@
7273
"NODETXPOOLACK",
7374
"notok",
7475
"Oidc",
76+
"oneofs",
7577
"onsi",
7678
"OpenAPI",
7779
"openethereum",
7880
"organisation",
7981
"parameterizable",
8082
"Postgres",
83+
"proto",
84+
"protobuf",
85+
"protoc",
8186
"protos",
8287
"RUSTC",
8388
"Secp",

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
# **/coverage/**
1515

1616
# typings/**
17+
18+
**/src/main/typescript/generated/proto/**

examples/cactus-example-carbon-accounting-backend/src/main/typescript/carbon-accounting-app.ts

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ export class CarbonAccountingApp {
231231
config.apiHost = addressInfoApi.address;
232232
config.cockpitHost = addressInfoCockpit.address;
233233
config.cockpitPort = addressInfoCockpit.port;
234+
config.grpcPort = 0; // TODO - make this configurable as well
234235
config.logLevel = this.options.logLevel || "INFO";
235236
}
236237

examples/cactus-example-carbon-accounting-backend/src/test/typescript/integration/admin-enroll-v1-endpoint.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ test(testCase, async (t: Test) => {
8181
apiSrvOpts.apiCorsDomainCsv = "*";
8282
apiSrvOpts.apiPort = 0;
8383
apiSrvOpts.cockpitPort = 0;
84+
apiSrvOpts.grpcPort = 0;
8485
apiSrvOpts.apiTlsEnabled = false;
8586
apiSrvOpts.plugins = [];
8687
const convictConfig = configService.newExampleConfigConvict(apiSrvOpts);

examples/cactus-example-carbon-accounting-frontend/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"@angular/compiler-cli": "12.1.1",
4646
"@angular/language-service": "12.1.1",
4747
"@ionic/angular-toolkit": "2.3.0",
48-
"@types/jasminewd2": "2.0.3",
4948
"@types/node": "12.11.1",
5049
"codelyzer": "6.0.2",
5150
"https-browserify": "1.0.0",

examples/cactus-example-supply-chain-backend/src/main/typescript/supply-chain-app.ts

+1
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ export class SupplyChainApp {
492492
properties.apiHost = addressInfoApi.address;
493493
properties.cockpitHost = addressInfoCockpit.address;
494494
properties.cockpitPort = addressInfoCockpit.port;
495+
properties.grpcPort = 0; // TODO - make this configurable as well
495496
properties.logLevel = this.options.logLevel || "INFO";
496497

497498
const apiServer = new ApiServer({

examples/cactus-example-supply-chain-frontend/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"@angular/compiler-cli": "12.1.1",
4747
"@angular/language-service": "12.1.1",
4848
"@ionic/angular-toolkit": "2.3.0",
49-
"@types/jasminewd2": "2.0.3",
5049
"@types/node": "12.11.1",
5150
"codelyzer": "6.0.2",
5251
"constants-browserify": "1.0.0",

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"start:example-supply-chain": "cd ./examples/supply-chain-app/ && npm i --no-package-lock && npm run start",
1919
"start:example-carbon-accounting": "CONFIG_FILE=examples/cactus-example-carbon-accounting-backend/example-config.json node examples/cactus-example-carbon-accounting-backend/dist/lib/main/typescript/carbon-accounting-app-cli.js",
2020
"purge-build-cache": "del-cli .build-cache/*",
21-
"clean": "npm run purge-build-cache && del-cli \"./{packages,examples,extensions}/cactus-*/{dist,.nyc_output,src/main/typescript/generated/openapi/typescript-axios/*}\"",
21+
"clean": "npm run purge-build-cache && del-cli \"./{packages,examples,extensions}/cactus-*/{dist,.nyc_output,src/main/proto/generated/*,src/main/typescript/generated/proto/protoc-gen-ts/*,src/main/typescript/generated/openapi/typescript-axios/*}\"",
2222
"lint": "eslint '*/*/src/**/*.{js,ts}' --quiet --fix && cspell \"*/*/src/**/*.{js,ts}\"",
2323
"tsc": "tsc --build --verbose",
2424
"codegen": "lerna run codegen",
@@ -79,7 +79,6 @@
7979
"@commitlint/config-conventional": "8.0.0",
8080
"@openapitools/openapi-generator-cli": "2.3.3",
8181
"@types/fs-extra": "9.0.11",
82-
"@types/jasminewd2": "2.0.10",
8382
"@types/node": "15.14.7",
8483
"@types/node-fetch": "2.5.4",
8584
"@types/tape": "4.13.0",
@@ -104,6 +103,8 @@
104103
"fs-extra": "10.0.0",
105104
"git-cz": "4.7.6",
106105
"globby": "10.0.2",
106+
"grpc-tools": "1.11.2",
107+
"grpc_tools_node_protoc_ts": "5.3.1",
107108
"husky": "4.2.5",
108109
"inquirer": "8.1.1",
109110
"json5": "2.2.0",
@@ -118,6 +119,7 @@
118119
"npm-run-all": "4.1.5",
119120
"npm-watch": "0.7.0",
120121
"prettier": "2.0.5",
122+
"protoc-gen-ts": "0.4.0",
121123
"run-time-error": "1.4.0",
122124
"secp256k1": "4.0.0",
123125
"shebang-loader": "0.0.1",

packages/cactus-cmd-api-server/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ const main = async () => {
116116
apiServerOptions.apiCorsDomainCsv = "your.domain.example.com";
117117
apiServerOptions.apiPort = 3000;
118118
apiServerOptions.cockpitPort = 3100;
119+
apiServerOptions.grpcPort = 5000;
119120
// Disble TLS (or provide TLS certs for secure HTTP if you are deploying to production)
120121
apiServerOptions.apiTlsEnabled = false;
121122
apiServerOptions.plugins = [

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

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"scripts": {
1515
"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",
1616
"codegen:openapi": "npm run generate-sdk",
17+
"proto:openapi": "openapi-generator-cli generate -i ./src/main/json/openapi.json -g protobuf-schema --model-name-suffix=PB --additional-properties=packageName=org.hyperledger.cactus.cmd_api_server -o ./src/main/proto/generated/openapi/ -t=./src/main/openapi-generator/templates/protobuf-schema/",
18+
"proto:protoc-gen-ts": "yarn run grpc_tools_node_protoc --plugin=protoc-gen-ts=../../node_modules/.bin/protoc-gen-ts --ts_out=grpc_js:./src/main/typescript/generated/proto/protoc-gen-ts/ --proto_path ./src/main/proto/generated/openapi/ ./src/main/proto/generated/openapi/services/*.proto",
19+
"codegen:proto": "run-s proto:openapi proto:protoc-gen-ts",
1720
"codegen": "run-p 'codegen:*'",
1821
"watch": "npm-watch",
1922
"webpack": "npm-run-all webpack:dev webpack:prod",
@@ -72,6 +75,8 @@
7275
},
7376
"homepage": "https://github.com/hyperledger/cactus#readme",
7477
"dependencies": {
78+
"@grpc/grpc-js": "1.3.6",
79+
"@grpc/proto-loader": "0.6.4",
7580
"@hyperledger/cactus-common": "0.8.0",
7681
"@hyperledger/cactus-core": "0.8.0",
7782
"@hyperledger/cactus-core-api": "0.8.0",
@@ -109,6 +114,7 @@
109114
"@types/express": "4.17.8",
110115
"@types/express-http-proxy": "1.6.1",
111116
"@types/express-jwt": "6.0.1",
117+
"@types/google-protobuf": "3.15.3",
112118
"@types/jsonwebtoken": "8.5.1",
113119
"@types/multer": "1.4.5",
114120
"@types/node-forge": "0.9.3",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{{>partial_header}}
2+
syntax = "proto3";
3+
4+
package {{{packageName}}};
5+
6+
import "google/protobuf/empty.proto";
7+
{{#imports}}
8+
{{#import}}
9+
import "{{{modelPackage}}}/{{{.}}}.proto";
10+
{{/import}}
11+
{{/imports}}
12+
13+
service {{classname}} {
14+
{{#operations}}
15+
{{#operation}}
16+
{{#description}}
17+
// {{{.}}}
18+
{{/description}}
19+
rpc {{operationId}} ({{#hasParams}}{{operationId}}Request{{/hasParams}}{{^hasParams}}google.protobuf.Empty{{/hasParams}}) returns ({{#vendorExtensions.x-grpc-response}}{{.}}{{/vendorExtensions.x-grpc-response}}{{^vendorExtensions.x-grpc-response}}{{operationId}}Response{{/vendorExtensions.x-grpc-response}});
20+
21+
{{/operation}}
22+
{{/operations}}
23+
}
24+
25+
{{#operations}}
26+
{{#operation}}
27+
{{#hasParams}}
28+
message {{operationId}}Request {
29+
{{#allParams}}
30+
{{#description}}
31+
// {{{.}}}
32+
{{/description}}
33+
{{#vendorExtensions.x-protobuf-type}}{{.}} {{/vendorExtensions.x-protobuf-type}}{{vendorExtensions.x-protobuf-data-type}} {{paramName}} = {{vendorExtensions.x-protobuf-index}};
34+
{{/allParams}}
35+
36+
}
37+
38+
{{/hasParams}}
39+
{{^vendorExtensions.x-grpc-response}}
40+
message {{operationId}}Response {
41+
{{{vendorExtensions.x-grpc-response-type}}} data = 1;
42+
}
43+
44+
{{/vendorExtensions.x-grpc-response}}
45+
{{/operation}}
46+
{{/operations}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{{>partial_header}}
2+
syntax = "proto3";
3+
4+
package {{{packageName}}};
5+
6+
{{#imports}}
7+
{{#import}}
8+
import "{{{modelPackage}}}/{{{import}}}.proto";
9+
{{/import}}
10+
{{/imports}}
11+
12+
{{#models}}
13+
{{#model}}
14+
message {{classname}} {
15+
{{#vars}}
16+
{{#description}}
17+
// {{{.}}}
18+
{{/description}}
19+
{{^isEnum}}
20+
{{#vendorExtensions.x-protobuf-type}}{{{.}}} {{/vendorExtensions.x-protobuf-type}}{{{vendorExtensions.x-protobuf-data-type}}} {{{name}}} = {{vendorExtensions.x-protobuf-index}}{{#vendorExtensions.x-protobuf-packed}} [packed=true]{{/vendorExtensions.x-protobuf-packed}};
21+
{{/isEnum}}
22+
{{#isEnum}}
23+
enum {{enumName}} {
24+
{{#allowableValues}}
25+
{{#enumVars}}
26+
{{{name}}} = {{{protobuf-enum-index}}};
27+
{{/enumVars}}
28+
{{/allowableValues}}
29+
}
30+
31+
{{enumName}} {{name}} = {{vendorExtensions.x-protobuf-index}};
32+
{{/isEnum}}
33+
34+
{{/vars}}
35+
}
36+
{{/model}}
37+
{{/models}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{>partial_header}}
2+
syntax = "proto3";
3+
4+
package {{{packageName}}};
5+
6+
{{#vendorExtensions.x-grpc-options}}
7+
option {{{.}}};
8+
{{/vendorExtensions.x-grpc-options}}
9+
10+
// Models
11+
{{#models}}
12+
{{#model}}
13+
import "{{modelPackage}}/{{classFilename}}.proto";
14+
{{/model}}
15+
{{/models}}
16+
17+
// APIs
18+
{{#apiInfo}}
19+
{{#apis}}
20+
import "{{apiPackage}}/{{classFilename}}.proto";
21+
{{/apis}}
22+
{{/apiInfo}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.openapi-generator-ignore
2+
README.md
3+
models/health_check_response_pb.proto
4+
models/memory_usage_pb.proto
5+
models/watch_healthcheck_v1_pb.proto
6+
services/default_service.proto
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5.1.1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# gPRC for org.hyperledger.cactus.cmd_api_server
2+
3+
Interact with a Cactus deployment through HTTP.
4+
5+
## Overview
6+
These files were generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
7+
8+
- API version: 0.0.1
9+
- Package version:
10+
- Build package: org.openapitools.codegen.languages.ProtobufSchemaCodegen
11+
12+
## Usage
13+
14+
Below are some usage examples for Go and Ruby. For other languages, please refer to https://grpc.io/docs/quickstart/.
15+
16+
### Go
17+
```
18+
# assuming `protoc-gen-go` has been installed with `go get -u github.com/golang/protobuf/protoc-gen-go`
19+
mkdir /var/tmp/go/
20+
protoc --go_out=/var/tmp/go/ services/*
21+
protoc --go_out=/var/tmp/go/ models/*
22+
```
23+
24+
### Ruby
25+
```
26+
# assuming `grpc_tools_ruby_protoc` has been installed via `gem install grpc-tools`
27+
RUBY_OUTPUT_DIR="/var/tmp/ruby/org.hyperledger.cactus.cmd_api_server"
28+
mkdir $RUBY_OUTPUT_DIR
29+
grpc_tools_ruby_protoc --ruby_out=$RUBY_OUTPUT_DIR --grpc_out=$RUBY_OUTPUT_DIR/lib services/*
30+
grpc_tools_ruby_protoc --ruby_out=$RUBY_OUTPUT_DIR --grpc_out=$RUBY_OUTPUT_DIR/lib models/*
31+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Hyperledger Cactus API
3+
4+
Interact with a Cactus deployment through HTTP.
5+
6+
The version of the OpenAPI document: 0.0.1
7+
8+
Generated by OpenAPI Generator: https://openapi-generator.tech
9+
*/
10+
11+
syntax = "proto3";
12+
13+
package org.hyperledger.cactus.cmd_api_server;
14+
15+
import "models/memory_usage_pb.proto";
16+
17+
message HealthCheckResponsePB {
18+
bool success = 256557056;
19+
20+
string createdAt = 61500732;
21+
22+
MemoryUsagePB memoryUsage = 335792418;
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Hyperledger Cactus API
3+
4+
Interact with a Cactus deployment through HTTP.
5+
6+
The version of the OpenAPI document: 0.0.1
7+
8+
Generated by OpenAPI Generator: https://openapi-generator.tech
9+
*/
10+
11+
syntax = "proto3";
12+
13+
package org.hyperledger.cactus.cmd_api_server;
14+
15+
16+
message MemoryUsagePB {
17+
float rss = 113234;
18+
19+
float heapTotal = 114487480;
20+
21+
float heapUsed = 30910521;
22+
23+
float external = 210148408;
24+
25+
float arrayBuffers = 116952168;
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Hyperledger Cactus API
3+
4+
Interact with a Cactus deployment through HTTP.
5+
6+
The version of the OpenAPI document: 0.0.1
7+
8+
Generated by OpenAPI Generator: https://openapi-generator.tech
9+
*/
10+
11+
syntax = "proto3";
12+
13+
package org.hyperledger.cactus.cmd_api_server;
14+
15+
16+
message WatchHealthcheckV1PB {
17+
}

0 commit comments

Comments
 (0)