/
client_build.go
43 lines (38 loc) · 1.17 KB
/
client_build.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
// Copyright 2021 Luis Guillén Civera <luisguillenc@gmail.com>. See LICENSE.
package finder
import (
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"
"github.com/luids-io/core/apiservice"
"github.com/luids-io/core/grpctls"
"github.com/luids-io/core/yalogi"
)
// ClientBuilder returns builder function for the apiservice
func ClientBuilder(opt ...ClientOption) apiservice.BuildFn {
return func(def apiservice.ServiceDef, logger yalogi.Logger) (apiservice.Service, error) {
//validates definition
err := def.Validate()
if err != nil {
return nil, err
}
opts := make([]grpc.DialOption, 0)
if def.Metrics {
opts = append(opts, grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor))
opts = append(opts, grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor))
}
//dial grpc
dial, err := grpctls.Dial(def.Endpoint, def.ClientCfg(), opts...)
if err != nil {
return nil, err
}
if def.Log {
opt = append(opt, SetLogger(logger))
}
//creates client
client := NewClient(dial, opt...)
return client, nil
}
}
func init() {
apiservice.RegisterBuilder(ServiceName(), ClientBuilder())
}