From def2baade5a4af58e3444cf6c923b1e4de584329 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Wed, 3 May 2023 12:22:27 -0600 Subject: add JoinDefaultPort for servers' configuration --- internal/server.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'internal/server.go') diff --git a/internal/server.go b/internal/server.go index 3efdf6e..e3fbfb2 100644 --- a/internal/server.go +++ b/internal/server.go @@ -3,6 +3,7 @@ package internal import ( "context" "net" + "strings" "sync" ) @@ -126,3 +127,19 @@ func (s *Server) propagateClose() { _ = s.Listener.Close() }() } + +// JoinDefaultPort appends ":" iff the address does not already contain a port. +func JoinDefaultPort(address string, port string) string { + if address[0] == '[' { + hend := strings.LastIndexByte(address, ']') + if len(address) > hend+1 && address[hend+1] == ':' { + return address + } + return net.JoinHostPort(address[1:hend], port) + } + + if strings.Contains(address, ":") { + return address + } + return net.JoinHostPort(address, port) +} -- cgit v1.2.3