forked from hidevopsio/hiboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hola.go
56 lines (47 loc) · 1.34 KB
/
hola.go
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
package controller
import (
"golang.org/x/net/context"
"hidevops.io/hiboot/examples/grpc/helloworld/protobuf"
"hidevops.io/hiboot/pkg/app"
"hidevops.io/hiboot/pkg/app/web"
"hidevops.io/hiboot/pkg/starter/grpc"
)
// controller
type holaController struct {
// embedded web.Controller
web.Controller
// declare HolaServiceClient
holaServiceClient protobuf.HolaServiceClient
}
// Init inject holaServiceClient
func newHolaController(holaServiceClient protobuf.HolaServiceClient) *holaController {
return &holaController{
holaServiceClient: holaServiceClient,
}
}
// GET /greeter/name/{name}
func (c *holaController) GetByName(name string) (response string) {
// call grpc server method
// pass context.Background() for the sake of simplicity
result, err := c.holaServiceClient.SayHola(context.Background(), &protobuf.HolaRequest{Name: name})
// got response
if err == nil {
response = result.Message
}
return
}
func init() {
// must: register grpc client, the name greeter-client should configured in application.yml
// see config/application-grpc.yml
//
// grpc:
// client:
// hello-world-service: # client name
// host: localhost # server host
// port: 7575 # server port
//
grpc.Client("hello-world-service",
protobuf.NewHolaServiceClient)
// must: register Controller
app.Register(newHolaController)
}