2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2020-02-15 17:09:24 +03:00
|
|
|
"net"
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2020-04-03 14:50:50 +03:00
|
|
|
"github.com/micro/go-micro/v2/auth"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/broker"
|
|
|
|
"github.com/micro/go-micro/v2/codec"
|
|
|
|
"github.com/micro/go-micro/v2/registry"
|
|
|
|
"github.com/micro/go-micro/v2/server"
|
|
|
|
"github.com/micro/go-micro/v2/transport"
|
2019-06-03 20:44:43 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/encoding"
|
|
|
|
)
|
|
|
|
|
|
|
|
type codecsKey struct{}
|
|
|
|
type grpcOptions struct{}
|
2020-02-15 17:09:24 +03:00
|
|
|
type netListener struct{}
|
|
|
|
type maxMsgSizeKey struct{}
|
2020-02-24 16:48:56 +03:00
|
|
|
type maxConnKey struct{}
|
2020-02-15 17:09:24 +03:00
|
|
|
type tlsAuth struct{}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
// gRPC Codec to be used to encode/decode requests for a given content type
|
|
|
|
func Codec(contentType string, c encoding.Codec) server.Option {
|
|
|
|
return func(o *server.Options) {
|
|
|
|
codecs := make(map[string]encoding.Codec)
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
2020-02-19 02:05:38 +03:00
|
|
|
if v, ok := o.Context.Value(codecsKey{}).(map[string]encoding.Codec); ok && v != nil {
|
|
|
|
codecs = v
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
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) server.Option {
|
2020-02-19 02:05:38 +03:00
|
|
|
return setServerOption(tlsAuth{}, t)
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 16:48:56 +03:00
|
|
|
// MaxConn specifies maximum number of max simultaneous connections to server
|
|
|
|
func MaxConn(n int) server.Option {
|
|
|
|
return setServerOption(maxConnKey{}, n)
|
|
|
|
}
|
|
|
|
|
2020-02-15 17:09:24 +03:00
|
|
|
// Listener specifies the net.Listener to use instead of the default
|
|
|
|
func Listener(l net.Listener) server.Option {
|
2020-02-19 02:05:38 +03:00
|
|
|
return setServerOption(netListener{}, l)
|
2020-02-15 17:09:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
// Options to be used to configure gRPC options
|
|
|
|
func Options(opts ...grpc.ServerOption) server.Option {
|
2020-02-19 02:05:38 +03:00
|
|
|
return setServerOption(grpcOptions{}, opts)
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// MaxMsgSize set the maximum message in bytes the server can receive and
|
|
|
|
// send. Default maximum message size is 4 MB.
|
|
|
|
//
|
|
|
|
func MaxMsgSize(s int) server.Option {
|
2020-02-19 02:05:38 +03:00
|
|
|
return setServerOption(maxMsgSizeKey{}, s)
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newOptions(opt ...server.Option) server.Options {
|
|
|
|
opts := server.Options{
|
2020-04-03 14:50:50 +03:00
|
|
|
Auth: auth.DefaultAuth,
|
2020-02-19 02:05:38 +03:00
|
|
|
Codecs: make(map[string]codec.NewCodec),
|
|
|
|
Metadata: map[string]string{},
|
|
|
|
Broker: broker.DefaultBroker,
|
|
|
|
Registry: registry.DefaultRegistry,
|
|
|
|
Transport: transport.DefaultTransport,
|
|
|
|
Address: server.DefaultAddress,
|
|
|
|
Name: server.DefaultName,
|
|
|
|
Id: server.DefaultId,
|
|
|
|
Version: server.DefaultVersion,
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opt {
|
|
|
|
o(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|