Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Return support for an scheme omitted IP address in RPC_HOST #199

Merged
merged 2 commits into from
Nov 29, 2023
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][])
Expand Down
13 changes: 11 additions & 2 deletions rpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/url"
"os"
"strings"

pb "github.com/anycable/anycable-go/protos"
)
Expand Down Expand Up @@ -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 "<invalid rpc host>"
return fmt.Sprintf("<invalid RPC host: %s>", c.Host)
}

if uri.Scheme == "http" || uri.Scheme == "https" {
Expand Down Expand Up @@ -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
}
10 changes: 9 additions & 1 deletion rpc/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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, "<invalid RPC host: invalid://:+>", c.Impl())
}