Skip to content

Commit 4bfe0e8

Browse files
committed
grpc example
1 parent 5b407e1 commit 4bfe0e8

File tree

11 files changed

+677
-14
lines changed

11 files changed

+677
-14
lines changed

example/grpc/config.toml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[app]
2+
runMode = "dev"
3+
name = "webapp"
4+
port = 18080
5+
6+
logProvider = "file"
7+
logPath = "/tmp"
8+
logRotate = true
9+
logRotateType = "day"
10+
logLimit = "100MB"
11+
12+
[session]
13+
enable = false
14+
provider = "file"
15+
16+
[mysql_manager]
17+
ping = false
18+
19+
[[mysql]]
20+
name="master"
21+
host="localhost"
22+
port=3306
23+
user="root"
24+
db="test"
25+
password=""
26+
read_only=false
27+
28+
[redis]
29+
host="localhost"
30+
port= 6379
31+
db = 0
32+
ping = false

example/grpc/main.go

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package main
22

3+
import (
4+
"github.com/silentred/toolkit/example/grpc/proto"
5+
"github.com/silentred/toolkit/interceptor"
6+
"github.com/silentred/toolkit/service"
7+
"golang.org/x/net/context"
8+
"google.golang.org/grpc"
9+
)
10+
11+
type helloSvc struct{}
12+
13+
// Hello implements helloworld.GreeterServer
14+
func (s *helloSvc) SayHello(ctx context.Context, in *proto.HelloReq) (*proto.HelloResp, error) {
15+
// log ctx values)
16+
return &proto.HelloResp{Message: "Hello " + in.Name}, nil
17+
}
18+
319
func main() {
20+
app := service.NewGrpcApp(nil)
21+
app.Initialize()
22+
23+
chain := interceptor.UnaryInterceptorChain(interceptor.NewRecovery(app.DefaultLogger()))
24+
opt := grpc.UnaryInterceptor(chain)
25+
s := grpc.NewServer(opt)
26+
27+
// register
28+
hello := &helloSvc{}
29+
proto.RegisterGreeterServer(s, hello)
30+
31+
// set service
32+
app.SetServer(s)
433

34+
app.ListenAndServe()
535
}

example/grpc/proto/gen.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
protoc -I=. --gofast_out=plugins=grpc:. hello.proto

0 commit comments

Comments
 (0)