frontend: deep-link via ?instance=; client/frontend default to :9090; Makefile help; v1.1.0
- cmd/frontend/web: honour ?instance=<hostname> query parameter on the initial scope hydration so /view/?instance=lb-ams opens the dashboard scoped to that maglevd. The cookie is updated on consumption; an unknown name still falls back to the first server via App.tsx. - cmd/client, cmd/frontend: --server now accepts bare hostnames. A new internal/netutil.EnsurePort canonicalises addresses by appending :9090 when no port is given, with bracketing for bare IPv6 literals. Unit test covers the IPv4/IPv6/bracketed/already-ported permutations. - Makefile: new self-documenting `help` target as the default rule; every user-facing target now carries a `## ` description that the awk-based help auto-extracts. fixstyle-web skips with a friendly message when prettier isn't installed instead of failing on npx. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package netutil holds tiny networking helpers shared across cmd/
|
||||
// binaries. Kept deliberately minimal — anything more involved than a
|
||||
// few lines belongs in its own package.
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// EnsurePort returns addr unchanged when it already carries a port, or
|
||||
// addr with ":<defaultPort>" appended otherwise. Bare IPv6 literals
|
||||
// ("2001:db8::1", "::1") are bracketed so the result is valid input to
|
||||
// grpc.NewClient and net.Dial. An empty addr is returned unchanged.
|
||||
func EnsurePort(addr, defaultPort string) string {
|
||||
if addr == "" {
|
||||
return addr
|
||||
}
|
||||
if _, _, err := net.SplitHostPort(addr); err == nil {
|
||||
return addr
|
||||
}
|
||||
// Already-bracketed bare IPv6 ("[::1]") falls through to the plain
|
||||
// concat at the bottom. Unbracketed IPv6 needs brackets first; we
|
||||
// detect it by handing the literal to net.ParseIP, which only
|
||||
// accepts the bare form (no brackets, no port).
|
||||
if ip := net.ParseIP(addr); ip != nil && ip.To4() == nil && strings.Contains(addr, ":") {
|
||||
return "[" + addr + "]:" + defaultPort
|
||||
}
|
||||
return addr + ":" + defaultPort
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package netutil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEnsurePort(t *testing.T) {
|
||||
const dp = "9090"
|
||||
cases := []struct {
|
||||
in, want string
|
||||
}{
|
||||
{"", ""},
|
||||
{"chbtl2", "chbtl2:9090"},
|
||||
{"chbtl2:9090", "chbtl2:9090"},
|
||||
{"chbtl2:1234", "chbtl2:1234"},
|
||||
{"lb-ams.example.com", "lb-ams.example.com:9090"},
|
||||
{"lb-ams.example.com:9090", "lb-ams.example.com:9090"},
|
||||
{"192.0.2.1", "192.0.2.1:9090"},
|
||||
{"192.0.2.1:9090", "192.0.2.1:9090"},
|
||||
{"::1", "[::1]:9090"},
|
||||
{"2001:db8::1", "[2001:db8::1]:9090"},
|
||||
{"[::1]:9090", "[::1]:9090"},
|
||||
{"[2001:db8::1]:443", "[2001:db8::1]:443"},
|
||||
// Already-bracketed but missing port: net.SplitHostPort rejects
|
||||
// it, ParseIP rejects the bracketed textual form, so the plain
|
||||
// concat path adds ":9090" and yields a valid host:port.
|
||||
{"[::1]", "[::1]:9090"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := EnsurePort(tc.in, dp)
|
||||
if got != tc.want {
|
||||
t.Errorf("EnsurePort(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user