Skip to content
Open
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: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ example/server/pkg
example/client/node_modules
example/client/helloworld
example/google
tests/luaunit.lua
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Lua.runtime.version": "LuaJIT",
"Lua.diagnostics.globals": [
"ngx"
],
"Lua.workspace.library": [
"${3rd}/OpenResty/library"
],
"Lua.workspace.checkThirdParty": false
}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.PHONY: test

test:
tests/luaunit.lua:
wget -q https://raw.githubusercontent.com/bluebird75/luaunit/master/luaunit.lua -o $@

test: tests/luaunit.lua
LUAUNIT=1 lua tests/test.lua
37 changes: 22 additions & 15 deletions example/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
FROM openresty/openresty:1.15.8.1rc1-xenial
FROM openresty/openresty:1.21.4.1-3-jammy

ARG PROTOC_VERSION="3.11.0"
RUN apt-get update \
&& apt-get install gcc git -y

RUN apt-get update -qq && \
apt-get install -y vim telnet git
## install basic google's protobuf files
ARG PROTOC_VERSION="21.6"
ARG TARGETOS
ARG TARGETARCH

RUN mkdir -p /var/log/nginx
RUN mkdir /etc/proto
COPY ./helloworld.proto /etc/proto/helloworld.proto
# normalize architechture
RUN case "${TARGETARCH}" in \
"amd64" ) DOCKER_ARCH="x86_64" ;; \
"arm/v7") DOCKER_ARCH="arm_hf" ;; \
"arm64" ) DOCKER_ARCH="aarch_64" ;; \
* ) DOCKER_ARCH="" ;; \
esac && \
PROTOC=protoc-${PROTOC_VERSION}-${TARGETOS}-${DOCKER_ARCH}.zip \
&& cd /tmp \
&& curl -fSL https://github.com/google/protobuf/releases/download/v${PROTOC_VERSION}/${PROTOC} -o ${PROTOC} \
&& unzip -d /usr/local ${PROTOC} \
&& chmod -R 655 /usr/local/include && chmod -R 755 /usr/local/bin/ \
&& rm -rf ${PROTOC}

## install basic google's protobuf files
RUN cd /opt && \
curl -OL https://github.com/google/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip && \
unzip -o protoc-${PROTOC_VERSION}-linux-x86_64.zip -d /usr/local/protoc && \
find /usr/local/protoc -type d -exec chmod 755 {} \; && \
find /usr/local/protoc/include -type f -exec chmod 644 {} \;
RUN luarocks install lua-protobuf
RUN luarocks install serpent

RUN luarocks install lua-resty-grpc-gateway
RUN luarocks install serpent
31 changes: 18 additions & 13 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
.PHONY: proto all helloworld.proto prepare docker
DOCKER_COMPOSE ?= docker compose

all: proto server

proto: helloworld.proto

clean:
rm server/helloworld/*.go
rm client/helloworld/*
$(RM) -r server/helloworld
$(RM) -r client/helloworld

prepare:
if [ ! -d "server/helloworld" ]; then mkdir -p server/helloworld; fi
if [ ! -d "client/helloworld" ]; then mkdir -p client/helloworld; fi
mkdir -p server/helloworld
mkdir -p client/helloworld

helloworld.proto: prepare
protoc -I. helloworld.proto \
--go_out=plugins=grpc:./server/helloworld \
proto: helloworld.proto prepare
protoc --proto_path=./ $< \
--go_out=./server/helloworld \
--go_opt=paths=source_relative \
--go_opt=M$<=helloworld/ \
--go-grpc_out=./server/helloworld \
--go-grpc_opt=paths=source_relative \
--go-grpc_opt=M$<=helloworld/ \
--grpc-web_out=import_style=commonjs+dts,mode=grpcweb:./client/helloworld \
--js_out=import_style=commonjs:./client/helloworld

server: proto
cd server && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app
CGO_ENABLED=0 go build -o app

docker: proto
$(DOCKER_COMPOSE) up -d --build

docker:
docker-compose up -d --build
.PHONY: all clean prepare proto server docker
6 changes: 5 additions & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ This is grpc-web gateway example with helloworld.

- Go (gRPC backend app)
- nodejs (gRPC frontend app)
- protoc related commands ([protobuf](https://github.com/protocolbuffers/protobuf/releases), [protoc-gen-grpc-web](https://github.com/grpc/grpc-web/releases))
- protoc related commands:
- [protobuf](https://github.com/protocolbuffers/protobuf/releases)
- [protoc-gen-grpc-web](https://github.com/grpc/grpc-web/releases)
- [protoc-gen-go-grpc](https://github.com/grpc/grpc-go)
- [protoc-gen-go](https://github.com/protocolbuffers/protobuf-go)
- Docker and docker-compose

## How to build
Expand Down
1 change: 1 addition & 0 deletions example/client/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.13.0
248 changes: 83 additions & 165 deletions example/client/dist/main.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions example/client/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__esModule = true;
var helloworld_grpc_web_pb_1 = require("./helloworld/helloworld_grpc_web_pb");
var helloworld_pb_1 = require("./helloworld/helloworld_pb");
var client = new helloworld_grpc_web_pb_1.GreeterClient("http://localhost:9000", null, null);
var request = new helloworld_pb_1.HelloRequest();
request.setName("grpc-web with gateway");
request.setDisplayname("grpc-web with gateway");
client.sayHello(request, {}, function (err, resp) {
if (err) {
console.error(err.message);
Expand Down
4 changes: 2 additions & 2 deletions example/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { HelloRequest, HelloReply } from "./helloworld/helloworld_pb"

const client = new GreeterClient("http://localhost:9000", null, null)
const request = new HelloRequest()
request.setName("grpc-web with gateway")
request.setDisplayname("grpc-web with gateway")

client.sayHello(request, {}, (err: grpcWeb.Error, resp: HelloReply) => {
client.sayHello(request, {}, (err: grpcWeb.RpcError, resp: HelloReply) => {
if (err) {
console.error(err.message)
return
Expand Down
18 changes: 9 additions & 9 deletions example/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@types/google-protobuf": "^3.2.7",
"google-protobuf": "^3.7.1",
"grpc": "^1.20.0",
"grpc-web": "^1.0.4",
"ts-loader": "^5.4.3",
"typescript": "^3.4.5",
"webpack": "^4.30.0",
"webpack-cli": "^3.3.1",
"webpack-dev-server": "^3.3.1"
"@grpc/grpc-js": "^1.9.0",
"@types/google-protobuf": "^3.15.6",
"google-protobuf": "^3.21.2",
"grpc-web": "^1.4.2",
"ts-loader": "^9.4.4",
"typescript": "^4.9.5",
"webpack": "^5.88.2",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.15.1"
},
"scripts": {
"build": "webpack",
Expand Down
5 changes: 4 additions & 1 deletion example/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"strict": true,
"allowJs": true,
"esModuleInterop": true
}
},
"files": [
"main.ts"
]
}
Loading