This commit is contained in:
Nex 2020-04-07 19:29:14 +08:00
parent f5b60712a9
commit a55de9c852
5 changed files with 81 additions and 65 deletions

View File

@ -1,34 +1,48 @@
# BDChain gRPC API 指南
## BDLedger gRPC API
## 编译 Go 版本 gRPC
### Generating gRPC code in languages
### 安装
#### Installling tools
- [Protocol Buffers 3](https://github.com/protocolbuffers/protobuf/releases)
- 下载对应版本,并将可执行程序 `protoc` 的目录加入到环境变量 `PATH`
- Go > 1.6
```bash
# 安装 Go 版本相关插件
go get -u google.golang.org/grpc
go get -u github.com/golang/protobuf/protoc-gen-go
```
- Node.js
```bash
npm install -g grpc-tools
```
- Web
```bash
git clone https://github.com/grpc/grpc-web
cd grpc-web
sudo make install-plugin
```
Download the [Protocol Buffers compiler](https://github.com/protocolbuffers/protobuf/releases) (protoc-3.11.4-*) for your platform and add `protoc` to `PATH` environment variable
参考资料:
For generating Go code:
```bash
GIT_TAG="v1.4.0-rc.4"
go get -d -u github.com/golang/protobuf/protoc-gen-go
git -C "$(go env GOPATH)"/src/github.com/golang/protobuf checkout $GIT_TAG
go install github.com/golang/protobuf/protoc-gen-go
```
(Update to google.golang.org/protobuf when https://github.com/grpc/grpc-go/pull/3435 resolves)
For generating Java code: From `bdledger-sdk-java` run `gradle generateProto`
For generating Node.js code:
```bash
npm install -g grpc-tools
```
For generating TypeScript definitions, also run:
```bash
npm install -g protoc-gen-ts
```
For frontend JavaScript:
```bash
git clone https://github.com/grpc/grpc-web
cd grpc-web
sudo make install-plugin
```
References:
- [Go Quick Start](https://grpc.io/docs/quickstart/go.html#prerequisites)
- [gRPC Go FAQ](https://github.com/grpc/grpc-go#faq)
### 编译
**TODO: Look into [protobuf.js](https://github.com/protobufjs/protobuf.js) for Nodes.js and frontend**
**TODO: Look into [Kroto+](https://github.com/marcoferrer/kroto-plus) for Kotlin**
- 在本项目打开控制台,或者切换工作路径到本项目
- 执行 `./scripts/gen-{{platform}}`
#### Generating code
Run
```bash
./scripts/gen [go] [nodejs] [ts]
```

View File

@ -1,20 +0,0 @@
@echo off
cd /d "%~dp0.."
set dir=.\gen\go
set exe=protoc
set gen=protoc-gen-go
if defined GOPATH set PATH=%PATH%;%GOPATH%\bin
where /q %exe% || echo missing %exe% && exit /B
where /q %gen% || echo missing %gen% && exit /B
if not exist %dir% mkdir %dir%
%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/common.proto
%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/error_details.proto
%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/node.proto
%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/ledger.proto
%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/query.proto
%exe% -I . --go_out=plugins=grpc:%dir% bdledger/api/contract.proto
echo all done

View File

@ -1,20 +0,0 @@
@echo off
cd /d "%~dp0.."
set dir=.\gen\nodejs
set exe=grpc_tools_node_protoc
set plugin=grpc_tools_node_protoc_plugin
where /q %exe% || echo missing %exe% && exit /B
where /q %plugin% || echo missing %plugin% && exit /B
for /f %%i in ('where %plugin%') do set pluginPath=%%i
if not exist %dir% mkdir %dir%
call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/common.proto
call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/error_details.proto
call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/node.proto
call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/ledger.proto
call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/query.proto
call %exe% -I . --js_out=import_style=commonjs,binary:%dir% --grpc_out=%dir% --plugin=protoc-gen-grpc=%pluginPath% bdledger/api/contract.proto
echo all done

37
scripts/gen.bat Normal file
View File

@ -0,0 +1,37 @@
@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0.."
where /q protoc || echo Missing protoc && exit /b
set gen=.\gen
set pkg=bdware/bdledger/api
set protos=%pkg%/common.proto %pkg%/error_details.proto %pkg%/node.proto %pkg%/ledger.proto %pkg%/query.proto %pkg%/contract.proto
for %%A in (%*) do (
if "%%A"=="go" (
set plugin=protoc-gen-go
where /q !plugin! || echo Missing !plugin! && exit /b
set out=%gen%\go
if not exist !out! mkdir !out!
echo Generating Go code
start /b protoc --go_out=plugins=grpc:!out! %protos%
)
if "%%A"=="nodejs" (
set plugin=grpc_tools_node_protoc_plugin
where /q !plugin! || echo missing !plugin! && exit /b
for /f %%i in ('where !plugin!') do set pluginPath=%%i
set out=%gen%\nodejs
if not exist !out! mkdir !out!
echo Generating Node.js code
start /b protoc --js_out=import_style=commonjs,binary:!nout! --grpc_out=!nout! --plugin=protoc-gen-grpc=!pluginPath! %protos%
)
if "%%A"=="ts" (
set plugin=protoc-gen-ts
where /q !plugin! || echo missing !plugin! && exit /B
for /f %%i in ('where !plugin!') do set pluginPath=%%i
set out=%gen%\nodejs
if not exist !out! mkdir !out!
echo Generating TypeScript definitions
start /b protoc --ts_out=!out! --plugin=protoc-gen-ts=!pluginPath! %protos%
)
)

5
scripts/gen.sh Normal file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
cd "${0%/*}/.."
# TODO