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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ In order to transform from REST to gRPC completely, you need to use three of hoo
```lua
## 0. prepare proto file import_paths
init_by_lua_block {
-- Define global import paths
PROTOC_IMPORT_PATHS = {
"/usr/local/include"
"/usr/local/protoc/include"
}
}

Expand Down Expand Up @@ -102,6 +103,7 @@ server {
ngx.header["Content-Type"] = "application/json"
}

grpc_set_header TE trailers;
grpc_set_header Content-Type application/grpc;
grpc_pass localhost:9000;
}
Expand Down
19 changes: 13 additions & 6 deletions example/gateway/conf/server.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lua_code_cache off;

## 0. prepare proto file import_paths
init_by_lua_block {
-- Define global import paths
PROTOC_IMPORT_PATHS = {
Expand All @@ -25,36 +26,42 @@ server {
}

location /rest {

## 1. Transform request from REST to gRPC
access_by_lua_block {
local proto = require("grpc-gateway.proto")
local gateway = require("grpc-gateway.request")
local grequest = require("grpc-gateway.request")
local p, err = proto.new("/etc/proto/helloworld.proto")
if err then
ngx.log(ngx.ERR, ("proto load error: %s"):format(err))
return
end
local req = gateway.new(p)
local req = grequest.new(p)
err = req:transform("helloworld.Greeter", "SayHello")
if err then
ngx.log(ngx.ERR, ("trasnform request error: %s"):format(err))
ngx.log(ngx.ERR, ("transform request error: %s"):format(err))
return
end
}

## 2. Transform response from gPRC to JSON
body_filter_by_lua_block {
local proto = require("grpc-gateway.proto")
local gateway = require("grpc-gateway.response")
local gresponse = require("grpc-gateway.response")
local p, err = proto.new("/etc/proto/helloworld.proto")
if err then
ngx.log(ngx.ERR, ("proto load error: %s"):format(err))
return
end
local resp = gateway.new(p)
local resp = gresponse.new(p)
err = resp:transform("helloworld.Greeter", "SayHello")
if err then
ngx.log(ngx.ERR, ("trasnform response error: %s"):format(err))
ngx.log(ngx.ERR, ("transform response error: %s"):format(err))
return
end
}

## 3. Swap response header to `Content-Type: application/json`
header_filter_by_lua_block {
ngx.header["Content-Type"] = "application/json"
}
Expand Down