Skip to content

Commit b02a7ab

Browse files
committed
fix: switch HTTP gateway dial to grpc.NewClient and add gRPC host config
1 parent e9c0bd1 commit b02a7ab

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type (
4444

4545
// GRPC contains configuration for the gRPC server.
4646
GRPC struct {
47+
Host string `mapstructure:"host"` // Host for the gRPC server
4748
Port string `mapstructure:"port"` // Port for the gRPC server
4849
TLSConfig TLSConfig `mapstructure:"tls"` // TLS configuration for the gRPC server
4950
}
@@ -293,6 +294,7 @@ func DefaultConfig() *Config {
293294
CORSAllowedHeaders: []string{"*"},
294295
},
295296
GRPC: GRPC{
297+
Host: "127.0.0.1",
296298
Port: "3478",
297299
TLSConfig: TLSConfig{
298300
Enabled: false,

internal/servers/server.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,7 @@ func (s *Container) Run(
258258
// Start the optional HTTP server with CORS and optional TLS configurations.
259259
// Connect to the gRPC server and register the HTTP handlers for each service.
260260
if srv.HTTP.Enabled {
261-
options := []grpc.DialOption{
262-
grpc.WithBlock(),
263-
}
261+
options := []grpc.DialOption{}
264262
if srv.GRPC.TLSConfig.Enabled {
265263
c, err := credentials.NewClientTLSFromFile(srv.GRPC.TLSConfig.CertPath, srv.NameOverride)
266264
if err != nil {
@@ -271,16 +269,14 @@ func (s *Container) Run(
271269
options = append(options, grpc.WithTransportCredentials(insecure.NewCredentials()))
272270
}
273271

274-
timeoutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
275-
defer cancel()
276-
277-
conn, err := grpc.DialContext(timeoutCtx, ":"+srv.GRPC.Port, options...)
272+
targetAddr := net.JoinHostPort(srv.GRPC.Host, srv.GRPC.Port) // gRPC server address
273+
conn, err := grpc.NewClient(targetAddr, options...) // Create gRPC client connection
278274
if err != nil {
279275
return err
280276
}
281277
defer func() {
282-
if err = conn.Close(); err != nil { // Connection close error
283-
slog.Error("Failed to close gRPC connection", slog.Any("error", err)) // Log close error
278+
if closeErr := conn.Close(); closeErr != nil { // Connection close error
279+
slog.Error("Failed to close gRPC connection", slog.Any("error", closeErr)) // Log close error
284280
}
285281
}()
286282

@@ -345,8 +341,8 @@ func (s *Container) Run(
345341
}).Handler(mux) // CORS handler created
346342

347343
httpServer = &http.Server{ // HTTP server configuration
348-
Addr: ":" + srv.HTTP.Port, // Server address
349-
Handler: corsHandler, // CORS handler
344+
Addr: targetAddr, // Server address
345+
Handler: corsHandler, // CORS handler
350346
ReadHeaderTimeout: 5 * time.Second,
351347
}
352348

0 commit comments

Comments
 (0)