Skip to content

Commit 3df47d2

Browse files
committed
feat(cmd-api-server): support grpc web services #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. 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 #1189 Signed-off-by: Peter Somogyvari <[email protected]>
1 parent 0e4c7c5 commit 3df47d2

File tree

58 files changed

+1718
-47
lines changed

Some content is hidden

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

58 files changed

+1718
-47
lines changed

.cspell.json

+9-2
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",
@@ -68,10 +69,17 @@
6869
"NODETXPOOLACK",
6970
"notok",
7071
"Oidc",
72+
"oneofs",
7173
"onsi",
7274
"OpenAPI",
7375
"openethereum",
7476
"organisation",
77+
"parameterizable",
78+
"pbjs",
79+
"pbts",
80+
"proto",
81+
"protobuf",
82+
"protoc",
7583
"protos",
7684
"RUSTC",
7785
"Secp",
@@ -91,8 +99,7 @@
9199
"uuidv",
92100
"vscc",
93101
"wasm",
94-
"Xdai",
95-
"parameterizable"
102+
"Xdai"
96103
],
97104
"dictionaries": [
98105
"typescript,node,npm,go,rust"

.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
@@ -44,7 +44,6 @@
4444
"@angular/compiler-cli": "12.1.1",
4545
"@angular/language-service": "12.1.1",
4646
"@ionic/angular-toolkit": "2.3.0",
47-
"@types/jasminewd2": "2.0.3",
4847
"@types/node": "12.11.1",
4948
"codelyzer": "6.0.2",
5049
"jasmine-core": "3.6.0",

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

+1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ export class SupplyChainApp {
497497
properties.apiHost = addressInfoApi.address;
498498
properties.cockpitHost = addressInfoCockpit.address;
499499
properties.cockpitPort = addressInfoCockpit.port;
500+
properties.grpcPort = 0; // TODO - make this configurable as well
500501
properties.logLevel = this.options.logLevel || "INFO";
501502

502503
const apiServer = new ApiServer({

examples/cactus-example-supply-chain-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
"jasmine-core": "3.6.0",

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"@commitlint/config-conventional": "8.0.0",
7777
"@openapitools/openapi-generator-cli": "2.3.3",
7878
"@types/fs-extra": "9.0.11",
79-
"@types/jasminewd2": "2.0.10",
8079
"@types/node-fetch": "2.5.4",
8180
"@types/tape": "4.13.0",
8281
"@types/tape-promise": "4.0.1",
@@ -100,6 +99,8 @@
10099
"fs-extra": "10.0.0",
101100
"git-cz": "4.7.6",
102101
"globby": "10.0.2",
102+
"grpc-tools": "1.11.2",
103+
"grpc_tools_node_protoc_ts": "5.3.1",
103104
"husky": "4.2.5",
104105
"inquirer": "8.1.1",
105106
"json5": "2.2.0",

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
"browser": "dist/cactus-cmd-api-server.web.umd.js",
1111
"browserMinified": "dist/cactus-cmd-api-server.web.umd.min.js",
1212
"module": "dist/lib/main/typescript/index.js",
13-
"types": "dist/types/main/typescript/index.d.ts",
13+
"types": "dist/lib/main/typescript/index.d.ts",
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 --additional-properties=packageName=org.hyperledger.cactus.cmd_api_server -o ./src/main/proto/generated/openapi/",
18+
"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",
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.7.0",
7681
"@hyperledger/cactus-core": "0.7.0",
7782
"@hyperledger/cactus-core-api": "0.7.0",
@@ -108,6 +113,7 @@
108113
"@types/express": "4.17.8",
109114
"@types/express-http-proxy": "1.6.1",
110115
"@types/express-jwt": "6.0.1",
116+
"@types/google-protobuf": "3.15.3",
111117
"@types/jsonwebtoken": "8.5.1",
112118
"@types/multer": "1.4.5",
113119
"@types/node-forge": "0.9.3",
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,5 @@
1+
README.md
2+
models/health_check_response.proto
3+
models/memory_usage.proto
4+
models/watch_healthcheck_v1.proto
5+
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,25 @@
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 public "models/memory_usage.proto";
16+
17+
message HealthCheckResponse {
18+
19+
bool success = 256557056;
20+
21+
string createdAt = 61500732;
22+
23+
MemoryUsage memoryUsage = 335792418;
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 MemoryUsage {
17+
18+
float rss = 113234;
19+
20+
float heapTotal = 114487480;
21+
22+
float heapUsed = 30910521;
23+
24+
float external = 210148408;
25+
26+
float arrayBuffers = 116952168;
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 WatchHealthcheckV1 {
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 "google/protobuf/empty.proto";
16+
import public "models/health_check_response.proto";
17+
18+
service DefaultService {
19+
rpc GetHealthCheckV1 (google.protobuf.Empty) returns (HealthCheckResponse);
20+
21+
rpc GetPrometheusMetricsV1 (google.protobuf.Empty) returns (GetPrometheusMetricsV1Response);
22+
23+
}
24+
25+
message GetPrometheusMetricsV1Response {
26+
string data = 1;
27+
}
28+

0 commit comments

Comments
 (0)