-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
56 lines (50 loc) · 1.33 KB
/
client.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 pubsubgrpc
import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"fmt"
"github.com/RTradeLtd/go-libp2p-pubsub-grpc/pb"
"github.com/RTradeLtd/grpc/dialer"
)
// Client is used to communicate
// with the gRPC Server service
type Client struct {
pb.PubSubServiceClient
conn *grpc.ClientConn
}
// NewClient is used to instantiate a pubsub grpc client
func NewClient(certPath, authKey, url string) (*Client, error) {
var (
dialOpts []grpc.DialOption
)
if certPath != "" {
creds, err := credentials.NewClientTLSFromFile(certPath, "")
if err != nil {
return nil, fmt.Errorf("could not load tls cert: %s", err)
}
dialOpts = append(dialOpts, grpc.WithTransportCredentials(creds))
if authKey != "" {
dialOpts = append(dialOpts,
grpc.WithPerRPCCredentials(dialer.NewCredentials(authKey, true)))
}
} else {
dialOpts = append(dialOpts, grpc.WithInsecure())
if authKey != "" {
dialOpts = append(dialOpts,
grpc.WithPerRPCCredentials(dialer.NewCredentials(authKey, false)))
}
}
conn, err := grpc.Dial(url, dialOpts...)
if err != nil {
return nil, err
}
return &Client{
conn: conn,
PubSubServiceClient: pb.NewPubSubServiceClient(conn),
}, nil
}
// Close is used to terminate our connection
// to the grpc server
func (c *Client) Close() error {
return c.conn.Close()
}