Repository navigation
Expand file tree
/
Copy pathserver.lua
More file actions
63 lines (56 loc) · 1.85 KB
/
Copy pathserver.lua
File metadata and controls
63 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- Run from the repository root:
-- lua examples/helloworld/server.lua 127.0.0.1 50051
package.path = "./?.lua;./?/init.lua;examples/helloworld/?.lua;" .. package.path
local grpc = require("grpc")
local hello = require("helloworld_grpc")
local host = arg[1] or "127.0.0.1"
local port = tonumber(arg[2]) or 50051
assert(grpc.load_descriptor_set("examples/helloworld/helloworld.pb"))
local server = grpc.new_server({
compression = os.getenv("GRPC_COMPRESSION"),
})
assert(hello.register_greeter_server(server, {
say_hello = function(_, request)
return { message = "Hello, " .. request.name .. "!" }
end,
watch_hello = function(_, request)
local greetings = { "Hello", "Welcome", "Goodbye" }
local index = 0
return function()
index = index + 1
if greetings[index] == nil then
return nil
end
return { message = greetings[index] .. ", " .. request.name .. "!" }
end
end,
send_hello = function(_, stream)
local count = 0
while true do
local request, recv_err = stream:recv()
assert(not recv_err, tostring(recv_err))
if not request then
break
end
count = count + 1
end
return { message = string.format("Received %d greeting(s)", count) }
end,
chat = function(_, stream)
while true do
local request, recv_err = stream:recv()
assert(not recv_err, tostring(recv_err))
if not request then
break
end
assert(stream:send({ message = "Echo: " .. request.name }))
end
end,
}))
assert(server:listen({
host = host,
port = port,
insecure = true,
}))
print(string.format("Hello World gRPC server listening on %s:%d", host, port))
assert(server:loop())