Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (

// Server contains the configurations for both HTTP and gRPC servers.
Server struct {
Host string `mapstructure:"host"` // Host for servers
HTTP HTTP `mapstructure:"http"` // HTTP server configuration
GRPC GRPC `mapstructure:"grpc"` // gRPC server configuration
NameOverride string `mapstructure:"name_override"`
Expand Down Expand Up @@ -283,6 +284,7 @@ func DefaultConfig() *Config {
AccountID: "",
Server: Server{
NameOverride: "",
Host: "127.0.0.1",
HTTP: HTTP{
Enabled: true,
Port: "3476",
Expand Down
21 changes: 9 additions & 12 deletions internal/servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ func (s *Container) Run(
// Start the optional HTTP server with CORS and optional TLS configurations.
// Connect to the gRPC server and register the HTTP handlers for each service.
if srv.HTTP.Enabled {
options := []grpc.DialOption{
grpc.WithBlock(),
}
options := []grpc.DialOption{}
if srv.GRPC.TLSConfig.Enabled {
c, err := credentials.NewClientTLSFromFile(srv.GRPC.TLSConfig.CertPath, srv.NameOverride)
if err != nil {
Expand All @@ -271,16 +269,14 @@ func (s *Container) Run(
options = append(options, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

timeoutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()

conn, err := grpc.DialContext(timeoutCtx, ":"+srv.GRPC.Port, options...)
targetAddr := net.JoinHostPort(srv.Host, srv.GRPC.Port) // gRPC server address
conn, err := grpc.NewClient(targetAddr, options...) // Create gRPC client connection
if err != nil {
return err
}
defer func() {
if err = conn.Close(); err != nil { // Connection close error
slog.Error("Failed to close gRPC connection", slog.Any("error", err)) // Log close error
if closeErr := conn.Close(); closeErr != nil { // Connection close error
slog.Error("Failed to close gRPC connection", slog.Any("error", closeErr)) // Log close error
}
}()

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

httpServer = &http.Server{ // HTTP server configuration
Addr: ":" + srv.HTTP.Port, // Server address
Handler: corsHandler, // CORS handler
httpTargetAddr := net.JoinHostPort(srv.Host, srv.HTTP.Port) // HTTP server address
httpServer = &http.Server{ // HTTP server configuration
Addr: httpTargetAddr, // Server address
Handler: corsHandler, // CORS handler
ReadHeaderTimeout: 5 * time.Second,
}

Expand Down