/
get_greeting.go
50 lines (44 loc) · 1.19 KB
/
get_greeting.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
package utils
import (
"context"
greet "github.com/hongbo-miao/hongbomiao.com/api-go/api/graphql_server/proto/greet/v1"
"github.com/rs/zerolog/log"
"go.elastic.co/apm/module/apmgrpc/v2"
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type Greeting struct {
Content string `json:"content"`
}
func GetGreeting(firstName string, lastName string) (*Greeting, error) {
req := &greet.GreetRequest{
FirstName: firstName,
LastName: lastName,
}
config := GetConfig()
conn, err := grpc.Dial(
config.GRPCServerHost+":"+config.GRPCServerPort,
grpc.WithUnaryInterceptor(apmgrpc.NewUnaryClientInterceptor()),
grpc.WithStatsHandler(new(ocgrpc.ClientHandler)),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Error().Err(err).Msg("grpc.Dial")
return nil, err
}
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.Error().Err(err).Msg("conn.Close")
}
}(conn)
c := greet.NewGreetServiceClient(conn)
res, err := c.Greet(context.Background(), req)
if err != nil {
log.Error().Err(err).Msg("c.Greet")
return nil, err
}
return &Greeting{
Content: res.Result,
}, nil
}