forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
38 lines (33 loc) · 938 Bytes
/
options.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
// Package grpc provides a gRPC options
package grpc
import (
"context"
"crypto/tls"
"github.com/micro/go-micro/client"
"google.golang.org/grpc"
)
type codecsKey struct{}
type tlsAuth struct{}
// gRPC Codec to be used to encode/decode requests for a given content type
func Codec(contentType string, c grpc.Codec) client.Option {
return func(o *client.Options) {
codecs := make(map[string]grpc.Codec)
if o.Context == nil {
o.Context = context.Background()
}
if v := o.Context.Value(codecsKey{}); v != nil {
codecs = v.(map[string]grpc.Codec)
}
codecs[contentType] = c
o.Context = context.WithValue(o.Context, codecsKey{}, codecs)
}
}
// AuthTLS should be used to setup a secure authentication using TLS
func AuthTLS(t *tls.Config) client.Option {
return func(o *client.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, tlsAuth{}, t)
}
}