diff --git a/CHANGELOG.md b/CHANGELOG.md index 0835abce..a2a8b5a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## master +- Return support for an scheme omitted IP address in RPC_HOST. ([@ardecvz][]) + +For example, `127.0.0.1:50051/anycable`. + +It's missing from 1.4.6 which introduced auto RPC implementation inferring from the RPC host. + ## 1.4.7 - Add NATS-based broker. ([@palkan][]) diff --git a/rpc/config.go b/rpc/config.go index 67b9e719..8f8be2fb 100644 --- a/rpc/config.go +++ b/rpc/config.go @@ -7,6 +7,7 @@ import ( "fmt" "net/url" "os" + "strings" pb "github.com/anycable/anycable-go/protos" ) @@ -73,10 +74,10 @@ func (c *Config) Impl() string { return c.Implementation } - uri, err := url.Parse(c.Host) + uri, err := url.Parse(ensureGrpcScheme(c.Host)) if err != nil { - return "" + return fmt.Sprintf("", c.Host) } if uri.Scheme == "http" || uri.Scheme == "https" { @@ -126,3 +127,11 @@ func (c *Config) TLSConfig() (*tls.Config, error) { return tlsConfig, nil } + +func ensureGrpcScheme(url string) string { + if strings.Contains(url, "://") { + return url + } + + return "grpc://" + url +} diff --git a/rpc/config_test.go b/rpc/config_test.go index 0df63216..6f9d7ce3 100644 --- a/rpc/config_test.go +++ b/rpc/config_test.go @@ -16,7 +16,6 @@ func TestConfig_Impl(t *testing.T) { assert.Equal(t, "trpc", c.Impl()) c.Implementation = "" - assert.Equal(t, "grpc", c.Impl()) c.Host = "http://localhost:8080/anycable" @@ -28,6 +27,15 @@ func TestConfig_Impl(t *testing.T) { c.Host = "grpc://localhost:50051/anycable" assert.Equal(t, "grpc", c.Impl()) + c.Host = "dns:///rpc:50051" + assert.Equal(t, "grpc", c.Impl()) + c.Host = "localhost:50051/anycable" assert.Equal(t, "grpc", c.Impl()) + + c.Host = "127.0.0.1:50051/anycable" + assert.Equal(t, "grpc", c.Impl()) + + c.Host = "invalid://:+" + assert.Equal(t, "", c.Impl()) }