2022-07-13 15:33:48 +00:00
|
|
|
package external
|
2022-03-22 12:40:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
agentmiddleware "github.com/hashicorp/consul/agent/grpc-middleware"
|
2022-03-22 12:40:24 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
|
|
|
)
|
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
// NewServer constructs a gRPC server for the external gRPC port, to which
|
2022-03-22 12:40:24 +00:00
|
|
|
// handlers can be registered.
|
|
|
|
func NewServer(logger agentmiddleware.Logger, tls *tlsutil.Configurator) *grpc.Server {
|
|
|
|
recoveryOpts := agentmiddleware.PanicHandlerMiddlewareOpts(logger)
|
|
|
|
|
|
|
|
opts := []grpc.ServerOption{
|
|
|
|
grpc.MaxConcurrentStreams(2048),
|
|
|
|
middleware.WithUnaryServerChain(
|
|
|
|
// Add middlware interceptors to recover in case of panics.
|
|
|
|
recovery.UnaryServerInterceptor(recoveryOpts...),
|
|
|
|
),
|
|
|
|
middleware.WithStreamServerChain(
|
|
|
|
// Add middlware interceptors to recover in case of panics.
|
|
|
|
recovery.StreamServerInterceptor(recoveryOpts...),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
if tls != nil && tls.GRPCTLSConfigured() {
|
|
|
|
creds := credentials.NewTLS(tls.IncomingGRPCConfig())
|
|
|
|
opts = append(opts, grpc.Creds(creds))
|
|
|
|
}
|
|
|
|
return grpc.NewServer(opts...)
|
|
|
|
}
|