From 40685999fbcd29a0982dcf751570e5c46a356b61 Mon Sep 17 00:00:00 2001 From: Gert Drapers <1533850+gertd@users.noreply.github.com> Date: Wed, 27 Nov 2024 09:43:26 -0800 Subject: [PATCH] fix gtw panic (#498) --- pkg/service/builder/service.go | 2 +- pkg/service/builder/service_factory.go | 14 +++++------ pkg/service/builder/service_manager.go | 34 ++++++++++++++------------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/pkg/service/builder/service.go b/pkg/service/builder/service.go index f749486d..2619de7f 100644 --- a/pkg/service/builder/service.go +++ b/pkg/service/builder/service.go @@ -25,7 +25,7 @@ type Service struct { Config *API Server *grpc.Server Listener net.Listener - Gateway Gateway + Gateway *Gateway Started chan bool Cleanup []func() } diff --git a/pkg/service/builder/service_factory.go b/pkg/service/builder/service_factory.go index e6d932dc..d5ea83c4 100644 --- a/pkg/service/builder/service_factory.go +++ b/pkg/service/builder/service_factory.go @@ -70,7 +70,7 @@ func (f *ServiceFactory) CreateService( return nil, err } - gateway := Gateway{} + var gateway *Gateway if gatewayOpts != nil && config.Gateway.ListenAddress != "" { gateway, err = f.prepareGateway(config, gatewayOpts) if err != nil { @@ -89,7 +89,7 @@ func (f *ServiceFactory) CreateService( } // prepareGateway provides a http server that will have the registrations pointed to the corresponding configured grpc server. -func (f *ServiceFactory) prepareGateway(config *API, gatewayOpts *GatewayOptions) (Gateway, error) { +func (f *ServiceFactory) prepareGateway(config *API, gatewayOpts *GatewayOptions) (*Gateway, error) { if len(config.Gateway.AllowedHeaders) == 0 { config.Gateway.AllowedHeaders = DefaultGatewayAllowedHeaders } @@ -113,7 +113,7 @@ func (f *ServiceFactory) prepareGateway(config *API, gatewayOpts *GatewayOptions if config.GRPC.Certs.HasCert() { tlsCreds, err := config.GRPC.Certs.ClientCredentials(true) if err != nil { - return Gateway{}, errors.Wrapf(err, "failed to get TLS credentials") + return nil, errors.Wrapf(err, "failed to get TLS credentials") } opts = append(opts, grpc.WithTransportCredentials(tlsCreds)) @@ -124,7 +124,7 @@ func (f *ServiceFactory) prepareGateway(config *API, gatewayOpts *GatewayOptions grpcEndpoint := fmt.Sprintf("dns:///%s", config.GRPC.ListenAddress) if err := gatewayOpts.HandlerRegistrations(context.Background(), runtimeMux, grpcEndpoint, opts); err != nil { - return Gateway{}, err + return nil, err } mux := http.NewServeMux() @@ -145,16 +145,16 @@ func (f *ServiceFactory) prepareGateway(config *API, gatewayOpts *GatewayOptions config.Gateway.HTTP = !config.Gateway.Certs.HasCert() if config.Gateway.HTTP { - return Gateway{Server: gtwServer, Mux: mux, Certs: nil}, nil + return &Gateway{Server: gtwServer, Mux: mux, Certs: nil}, nil } tlsServerConfig, err := config.Gateway.Certs.ServerConfig() if err != nil { - return Gateway{Server: gtwServer, Mux: mux, Certs: &config.Gateway.Certs}, err + return nil, err } gtwServer.TLSConfig = tlsServerConfig - return Gateway{Server: gtwServer, Mux: mux, Certs: &config.Gateway.Certs}, nil + return &Gateway{Server: gtwServer, Mux: mux, Certs: &config.Gateway.Certs}, nil } // gatewayMux creates a gateway multiplexer for serving the API as an OpenAPI endpoint. diff --git a/pkg/service/builder/service_manager.go b/pkg/service/builder/service_manager.go index 9bdece1e..e86260d2 100644 --- a/pkg/service/builder/service_manager.go +++ b/pkg/service/builder/service_manager.go @@ -159,23 +159,25 @@ func (s *ServiceManager) StartServers(ctx context.Context) error { return grpcServer.Serve(listener) }) - httpServer := serverDetails.Gateway - if httpServer.Server != nil { - s.errGroup.Go(func() error { - s.logger.Info().Msgf("Starting %s gateway server", httpServer.Server.Addr) - if httpServer.Certs.HasCert() { - err := httpServer.Server.ListenAndServeTLS(httpServer.Certs.Cert, httpServer.Certs.Key) - if err != nil { - return err + if serverDetails.Gateway != nil { + httpServer := serverDetails.Gateway + if httpServer.Server != nil { + s.errGroup.Go(func() error { + s.logger.Info().Msgf("Starting %s gateway server", httpServer.Server.Addr) + if httpServer.Certs.HasCert() { + err := httpServer.Server.ListenAndServeTLS(httpServer.Certs.Cert, httpServer.Certs.Key) + if err != nil { + return err + } + } else { + err := httpServer.Server.ListenAndServe() + if err != nil { + return err + } } - } else { - err := httpServer.Server.ListenAndServe() - if err != nil { - return err - } - } - return nil - }) + return nil + }) + } } serverDetails.Started <- true // send started information.