Skip to content

Commit

Permalink
fix gtw panic (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
gertd authored Nov 27, 2024
1 parent df63b9a commit 4068599
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pkg/service/builder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Service struct {
Config *API
Server *grpc.Server
Listener net.Listener
Gateway Gateway
Gateway *Gateway
Started chan bool
Cleanup []func()
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/service/builder/service_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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))
Expand All @@ -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()
Expand All @@ -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.
Expand Down
34 changes: 18 additions & 16 deletions pkg/service/builder/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4068599

Please sign in to comment.