Skip to content

Commit d31403e

Browse files
fix: read the listen port from a bare host:port self endpoint (#36)
Codefly hands a service its own endpoint as a bare host:port (a rendered cell writes "localhost:8080"); url.Parse reads that as scheme "localhost" with no port, so every rendered solution refused to boot with 'unresolved listen port ""'. listenPort accepts both the bare form and a URL. Co-authored-by: Antoine Toussaint <312536060+antoinetoussaint-byte@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 0a997ca commit d31403e

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎listen_port_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package solution
2+
3+
import "testing"
4+
5+
// TestListenPortReadsEitherAddressForm pins the two shapes Codefly hands a
6+
// service for its own endpoint: a rendered cell writes the bare host:port, a
7+
// local run a URL. The bare form is the one url.Parse misreads as a scheme.
8+
func TestListenPortReadsEitherAddressForm(t *testing.T) {
9+
for address, want := range map[string]string{
10+
"localhost:8080": "8080",
11+
"http://localhost:8080": "8080",
12+
"http://127.0.0.1:41234": "41234",
13+
"[::1]:9000": "9000",
14+
"localhost": "",
15+
"": "",
16+
} {
17+
if got := listenPort(address); got != want {
18+
t.Errorf("listenPort(%q) = %q, want %q", address, got, want)
19+
}
20+
}
21+
}

‎solution.go‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ func env(key, fallback string) string {
256256
// could not resolve it. Using the SDK keeps addresses and ports out of the
257257
// runtime: they come from Codefly's runtime-injected endpoint map (or, in a
258258
// local run, its deterministic native workspace map) — never a hardcoded port.
259+
// listenPort is the port of this service's own endpoint address. Codefly hands a
260+
// service its own endpoint as a bare "host:port" (a rendered cell writes
261+
// "localhost:8080"), and other endpoints as URLs; url.Parse reads the bare form
262+
// as scheme "localhost" with no port, which left every rendered solution
263+
// refusing to boot with an empty listen port. Both forms resolve here.
264+
func listenPort(self string) string {
265+
if strings.Contains(self, "://") {
266+
if u, err := url.Parse(self); err == nil {
267+
return u.Port()
268+
}
269+
return ""
270+
}
271+
if _, port, err := net.SplitHostPort(self); err == nil {
272+
return port
273+
}
274+
return ""
275+
}
276+
259277
func address(ctx context.Context, module, service, endpoint, api string) string {
260278
q := codefly.For(ctx).Endpoint(endpoint)
261279
if module != "" {
@@ -386,9 +404,7 @@ func loadConfig(ctx context.Context, id string) config {
386404
port := env("PORT", "")
387405
if port == "" {
388406
if self := address(ctx, "", "", "http", "http"); self != "" {
389-
if u, err := url.Parse(self); err == nil {
390-
port = u.Port()
391-
}
407+
port = listenPort(self)
392408
}
393409
}
394410
public := env("PUBLIC_URL", "")

0 commit comments

Comments
 (0)