Skip to content

Commit 53d0b59

Browse files
authored
Merge pull request #2793 from Permify/ufuk/configurable-grpc-host
fix: switch HTTP gateway dial to grpc.NewClient and add gRPC host config
2 parents c139f98 + f6db957 commit 53d0b59

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

internal/config/config.go

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

2828
// Server contains the configurations for both HTTP and gRPC servers.
2929
Server struct {
30+
Host string `mapstructure:"host"` // Host for servers
3031
HTTP HTTP `mapstructure:"http"` // HTTP server configuration
3132
GRPC GRPC `mapstructure:"grpc"` // gRPC server configuration
3233
NameOverride string `mapstructure:"name_override"`
@@ -283,6 +284,7 @@ func DefaultConfig() *Config {
283284
AccountID: "",
284285
Server: Server{
285286
NameOverride: "",
287+
Host: "127.0.0.1",
286288
HTTP: HTTP{
287289
Enabled: true,
288290
Port: "3476",

internal/servers/server.go

Lines changed: 9 additions & 12 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.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

@@ -344,9 +340,10 @@ func (s *Container) Run(
344340
}, // Methods configured
345341
}).Handler(mux) // CORS handler created
346342

347-
httpServer = &http.Server{ // HTTP server configuration
348-
Addr: ":" + srv.HTTP.Port, // Server address
349-
Handler: corsHandler, // CORS handler
343+
httpTargetAddr := net.JoinHostPort(srv.Host, srv.HTTP.Port) // HTTP server address
344+
httpServer = &http.Server{ // HTTP server configuration
345+
Addr: httpTargetAddr, // Server address
346+
Handler: corsHandler, // CORS handler
350347
ReadHeaderTimeout: 5 * time.Second,
351348
}
352349

0 commit comments

Comments
 (0)