forked from kazegusuri/grpcurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
29 lines (24 loc) · 877 Bytes
/
conn.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
package main
import (
"github.com/jhump/protoreflect/grpcreflect"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)
func NewGRPCConnection(ctx context.Context, addr, userAgent string, insecure bool) (*grpc.ClientConn, error) {
var dialOpts []grpc.DialOption
if insecure {
dialOpts = append(dialOpts, grpc.WithInsecure())
} else {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
}
if userAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(userAgent))
}
return grpc.DialContext(ctx, addr, dialOpts...)
}
func NewServerReflectionClient(ctx context.Context, conn *grpc.ClientConn) *grpcreflect.Client {
cli := rpb.NewServerReflectionClient(conn)
return grpcreflect.NewClient(ctx, cli)
}