Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion openapi/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
gateway
go.sum
21 changes: 1 addition & 20 deletions openapi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,4 @@ clean:

.PHONY: install
install:
go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
go mod tidy

.PHONY: gen_protobuf
gen_protobuf:
${PROTOC} \
--go_out ${GENERATED_DIR} --go_opt paths=source_relative \
--go-grpc_out ${GENERATED_DIR} --go-grpc_opt paths=source_relative \

.PHONY: gen_gateway
gen_gateway:
${PROTOC} \
--grpc-gateway_out ${GENERATED_DIR} \
--grpc-gateway_opt logtostderr=true \
--grpc-gateway_opt paths=source_relative \
--grpc-gateway_opt generate_unbound_methods=true \
./install.sh
3 changes: 2 additions & 1 deletion openapi/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# GO gRPC Gateway
# GO gRPC Gateway with OpenAPI Specification

- gRPC Gateway: <https://github.com/grpc-ecosystem/grpc-gateway>
- Template: <https://github.com/johanbrandhorst/grpc-gateway-boilerplate>
- Google APIs Proto Files: Node.js Client: <https://github.com/googleapis/nodejs-proto-files>
46 changes: 0 additions & 46 deletions openapi/gateway.go

This file was deleted.

2 changes: 1 addition & 1 deletion openapi/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/wechaty/go-grpc/openapi
module github.com/wechaty/grpc/openapi

go 1.16

Expand Down
33 changes: 33 additions & 0 deletions openapi/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

set -e

# https://stackoverflow.com/a/4774063/1123955
SCRIPTPATH="$( cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 ; pwd -P )"
THIRD_PARTY_DIR="${SCRIPTPATH}/../third-party/"

function go_install () {
go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
}

function install_proto_protoc_gen_openapiv2 () {
if [ -d ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options ]; then
echo "install skipped: ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options exists"
return
fi

mkdir -p ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options
curl https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-openapiv2/options/annotations.proto > ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options/annotations.proto
curl https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-openapiv2/options/openapiv2.proto > ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options/openapiv2.proto
}

main () {
go_install
install_proto_protoc_gen_openapiv2
}

main
10 changes: 0 additions & 10 deletions openapi/main.go

This file was deleted.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-grpc",
"version": "0.20.4",
"version": "0.20.5",
"description": "gRPC for Wechaty",
"main": "dist/src/mod.js",
"typings": "dist/src/mod.d.js",
Expand All @@ -10,7 +10,8 @@
},
"scripts": {
"clean": "shx rm -fr dist/* generated/*",
"dist": "npm-run-all clean generate && tsc && shx mv generated/ dist/",
"dist": "npm-run-all clean generate tsc dist:cp ",
"dist:cp": "shx cp -R generated/ dist/ && shx cp -R proto/ dist/",
"dist:py": "python3 setup.py sdist bdist_wheel",
"publish:py": "twine upload dist/*",
"example:server": "nodemon --exec ts-node examples/server.ts",
Expand All @@ -21,6 +22,7 @@
"lint:ts": "tsc --noEmit",
"lint:proto": "bash -c 'protoc -I third-party -I proto/wechaty --lint_out=. $(find proto/ -type f -name *.proto)'",
"install:protoc": "bash -x scripts/install-protoc.sh",
"tsc": "tsc",
"test": "npm run lint && npm run test:unit",
"test:pack": "bash -x scripts/npm-pack-testing.sh",
"test:unit": "blue-tape -r ts-node/register \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\"",
Expand Down
101 changes: 63 additions & 38 deletions scripts/generate-stub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,66 @@ PROTOC_CMD="protoc \
--proto_path=${PROTO_DIR}/wechaty \
$PROTO_FILE_LIST"

#
# 1. JS for Protocol Buffer
# - https://github.com/google/protobuf/releases/latest
#
# Generate: wechaty-puppet_pb.js
$PROTOC_CMD \
--js_out="import_style=commonjs,binary:${OUT_DIR}"

#
# 2. JS for gRPC Stubs
# - https://www.npmjs.com/package/grpc-tools
#
# Generate: wechaty-puppet_grpc_pb.js
$PROTOC_CMD \
--plugin="protoc-gen-grpc=node_modules/.bin/grpc_tools_node_protoc_plugin" \
--grpc_out="${OUT_DIR}"

#
# 3. TS for Protocol Buffer & gRPC Stubs
# - https://github.com/agreatfool/grpc_tools_node_protoc_ts
#
# Generate:
# wechaty-puppet_grpc_pb.d.ts
# wechaty-puppet_pb.d.ts
$PROTOC_CMD \
--plugin="protoc-gen-grpc=node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts" \
--grpc_out="${OUT_DIR}"

#
# 4. JS & TS for gRPC Web
# - https://github.com/improbable-eng/ts-protoc-gen
#
# Generate:
# wechaty-puppet_pb_service.d.ts
# wechaty-puppet_pb_service.js
$PROTOC_CMD \
--plugin="protoc-gen-ts=node_modules/ts-protoc-gen/bin/protoc-gen-ts" \
--ts_out="service=grpc-web:${OUT_DIR}"
function gen_js_pb () {
#
# 1. JS for Protocol Buffer
# - https://github.com/google/protobuf/releases/latest
#
# Generate: wechaty-puppet_pb.js
$PROTOC_CMD \
--js_out="import_style=commonjs,binary:${OUT_DIR}"
}

function gen_js_stub () {
#
# 2. JS for gRPC Stubs
# - https://www.npmjs.com/package/grpc-tools
#
# Generate: wechaty-puppet_grpc_pb.js
$PROTOC_CMD \
--plugin="protoc-gen-grpc=node_modules/.bin/grpc_tools_node_protoc_plugin" \
--grpc_out="${OUT_DIR}"
}

function gen_ts_typing () {
#
# 3. TS for Protocol Buffer & gRPC Stubs
# - https://github.com/agreatfool/grpc_tools_node_protoc_ts
#
# Generate:
# wechaty-puppet_grpc_pb.d.ts
# wechaty-puppet_pb.d.ts
$PROTOC_CMD \
--plugin="protoc-gen-grpc=node_modules/grpc_tools_node_protoc_ts/bin/protoc-gen-ts" \
--grpc_out="${OUT_DIR}"
}

function gen_web_grpc () {
#
# 4. JS & TS for gRPC Web
# - https://github.com/improbable-eng/ts-protoc-gen
#
# Generate:
# wechaty-puppet_pb_service.d.ts
# wechaty-puppet_pb_service.js
$PROTOC_CMD \
--plugin="protoc-gen-ts=node_modules/ts-protoc-gen/bin/protoc-gen-ts" \
--ts_out="service=grpc-web:${OUT_DIR}"
}
function gen_openapi () {
pushd openapi
make generate
popd
}

function main () {
gen_js_pb
gen_js_stub
gen_ts_typing

gen_web_grpc

gen_openapi
}

main
19 changes: 7 additions & 12 deletions scripts/install-protoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ function install_proto_google_api () {
curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto > ${THIRD_PARTY_DIR}/google/api/http.proto
}

function install_protoc_gen_openapiv2 () {
pushd openapi
make install
popd
}

# function install_google_protobuf_wrappers () {
# if [ -d ${THIRD_PARTY_DIR}/google/protobuf ]; then
# echo "install skipped: ${THIRD_PARTY_DIR}/google/protobuf exists"
Expand All @@ -73,25 +79,14 @@ function install_proto_google_api () {
# https://raw.githubusercontent.com/protocolbuffers/protobuf/master/src/google/protobuf/descriptor.proto
# }

function install_proto_protoc_gen_openapiv2 () {
if [ -d ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options ]; then
echo "install skipped: ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options exists"
return
fi

mkdir -p ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options
curl https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-openapiv2/options/annotations.proto > ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options/annotations.proto
curl https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/protoc-gen-openapiv2/options/openapiv2.proto > ${THIRD_PARTY_DIR}/protoc-gen-openapiv2/options/openapiv2.proto
}

function main () {
install_protoc
check_protoc_version

install_protoc_gen_lint
install_protoc_gen_openapiv2

install_proto_google_api
install_proto_protoc_gen_openapiv2
}

main
12 changes: 12 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
export { VERSION } from './version'

enum MajorVersionEnum {
v0 = 'v0',
// v1 = 'v1',
}

export type ApiStore = {
[key in MajorVersionEnum]: {
file: string,
data: string,
}
}
12 changes: 9 additions & 3 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
export * from './generated'

export {
import * as proto from './proto'
import * as openApi from './openapi'
import {
VERSION,
} from './config'

export {
proto,
openApi,
VERSION,
}
export * from './generated'
export {
StringValue,
} from 'google-protobuf/google/protobuf/wrappers_pb'
23 changes: 23 additions & 0 deletions src/openapi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env ts-node

// tslint:disable:no-shadowed-variable
import test from 'tstest'

import {
puppet,
} from './openapi'

test('puppet.v0.file', async (t) => {
const EXPECTED_PATH_REG = /puppet.swagger.json$/
t.true(EXPECTED_PATH_REG.test(puppet.v0.file), 'puppet OpenAPI file should be puppet.swagger.json')
})

test('puppet.v0.data', async (t) => {
t.true(puppet.v0.data.length > 0, 'should get JSON data for puppet.v0.data')
try {
const obj = JSON.parse(puppet.v0.data)
t.true(obj, 'should be able to parse JSON for puppet.v0.data')
} catch (e) {
t.fail('should be able to parse JSON data for puppet.v0.data')
}
})
29 changes: 29 additions & 0 deletions src/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'path'
import fs from 'fs'

import { ApiStore } from './config'

function getOpenApiPath (...paths: string[]): string {
return path.join(
__dirname,
'..',
'generated',
...paths,
)
}

const puppetSwaggerFile = getOpenApiPath(
'wechaty',
'puppet.swagger.json',
)

const puppet: ApiStore = {
v0: {
data: fs.readFileSync(puppetSwaggerFile).toString(),
file: puppetSwaggerFile,
},
}

export {
puppet,
}
Loading