Move TLS config to Server struct

This commit is contained in:
Rob Watson 2021-11-26 20:01:34 +01:00
parent abf5398d24
commit 817a10d269
1 changed files with 6 additions and 13 deletions

View File

@ -18,7 +18,6 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/durationpb"
) )
@ -273,6 +272,10 @@ func Start(options Options) error {
log.Infof("Listening at %s", options.Config.BindAddr) log.Infof("Listening at %s", options.Config.BindAddr)
if options.Config.TLSCertFile != "" && options.Config.TLSKeyFile != "" {
return httpServer.ListenAndServeTLS(options.Config.TLSCertFile, options.Config.TLSKeyFile)
}
return httpServer.ListenAndServe() return httpServer.ListenAndServe()
} }
@ -300,18 +303,8 @@ func buildGRPCServer(c config.Config, logger *zap.Logger) (*grpc.Server, error)
streamInterceptors = append(streamInterceptors, grpcrecovery.StreamServerInterceptor(panicOpts...)) streamInterceptors = append(streamInterceptors, grpcrecovery.StreamServerInterceptor(panicOpts...))
} }
options := []grpc.ServerOption{ return grpc.NewServer(
grpc.StreamInterceptor(grpcmiddleware.ChainStreamServer(streamInterceptors...)), grpc.StreamInterceptor(grpcmiddleware.ChainStreamServer(streamInterceptors...)),
grpc.UnaryInterceptor(grpcmiddleware.ChainUnaryServer(unaryInterceptors...)), grpc.UnaryInterceptor(grpcmiddleware.ChainUnaryServer(unaryInterceptors...)),
} ), nil
if c.TLSCertFile != "" && c.TLSKeyFile != "" {
creds, err := credentials.NewServerTLSFromFile(c.TLSCertFile, c.TLSKeyFile)
if err != nil {
return nil, fmt.Errorf("error building credentials: %v", err)
}
options = append(options, grpc.Creds(creds))
}
return grpc.NewServer(options...), nil
} }