forked from octofoxio/foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc_dial.go
More file actions
62 lines (55 loc) · 1.66 KB
/
grpc_dial.go
File metadata and controls
62 lines (55 loc) · 1.66 KB
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
57
58
59
60
61
62
/*
* Copyright (c) 2019. Octofox.io
*/
package foundation
import (
"context"
"github.com/octofoxio/foundation/logger"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"time"
)
func MakeDialOrPanic(endpoint string) *grpc.ClientConn {
if conn, err := MakeDial(endpoint); err != nil {
panic(err)
} else {
return conn
}
}
func WithAuthorizationDialOption(accessToken string) grpc.DialOption {
return grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
err := invoker(ctx, method, req, reply, cc, opts...)
ctx = metadata.AppendToOutgoingContext(ctx, GRPC_METADATA_AUTHORIZATION_KEY, accessToken)
return err
})
}
func MakeDial(endpoint string, dialOptions ...grpc.DialOption) (*grpc.ClientConn, error) {
var log = logger.New("grpc").WithServiceID("foundation").WithServiceInfo("grpc")
options := []grpc.DialOption{
grpc.WithKeepaliveParams(
keepalive.ClientParameters{
Time: 50 * time.Second,
PermitWithoutStream: true,
}),
}
for _, o := range dialOptions {
options = append(options, o)
}
if certPath := EnvString(OCTOFOX_FOUNDATION_GRPC_CERT, ""); certPath != "" {
creds, err := credentials.NewClientTLSFromFile(certPath, "")
if err != nil {
panic(err)
}
options = append(options, grpc.WithTransportCredentials(creds))
} else {
// IF NO GRPC CERT
// SKIP TO USE Insecure
log.Warn("GRPC Connect dial with insecure mode")
options = append(options, grpc.WithInsecure())
}
return grpc.Dial(endpoint,
options...,
)
}