diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-05-03 12:22:27 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-05-03 12:22:27 -0600 |
commit | def2baade5a4af58e3444cf6c923b1e4de584329 (patch) | |
tree | a07811cc1aa87be328b5a118fe024f9999ddfb1e | |
parent | c7f8a1292544c8981eb1c35394d5b50100fe1790 (diff) |
add JoinDefaultPort for servers' configuration
-rw-r--r-- | finger/serve.go | 6 | ||||
-rw-r--r-- | gemini/serve.go | 5 | ||||
-rw-r--r-- | gopher/serve.go | 6 | ||||
-rw-r--r-- | internal/server.go | 17 | ||||
-rw-r--r-- | spartan/serve.go | 6 |
5 files changed, 25 insertions, 15 deletions
diff --git a/finger/serve.go b/finger/serve.go index 5675dcf..1bc69b8 100644 --- a/finger/serve.go +++ b/finger/serve.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "net" - "strings" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/internal" @@ -30,9 +29,8 @@ func NewServer( ) (sr.Server, error) { fs := &fingerServer{handler: handler} - if strings.IndexByte(hostname, ':') < 0 { - hostname = net.JoinHostPort(hostname, "79") - } + hostname = internal.JoinDefaultPort(hostname, "79") + address = internal.JoinDefaultPort(address, "79") var err error fs.Server, err = internal.NewServer(ctx, hostname, network, address, errLog, fs.handleConn) diff --git a/gemini/serve.go b/gemini/serve.go index 01e186f..a439ce0 100644 --- a/gemini/serve.go +++ b/gemini/serve.go @@ -38,9 +38,8 @@ func NewServer( ) (sr.Server, error) { s := &server{handler: handler} - if strings.IndexByte(hostname, ':') < 0 { - hostname = net.JoinHostPort(hostname, "1965") - } + hostname = internal.JoinDefaultPort(hostname, "1965") + address = internal.JoinDefaultPort(address, "1965") internalServer, err := internal.NewServer(ctx, hostname, network, address, errorLog, s.handleConn) if err != nil { diff --git a/gopher/serve.go b/gopher/serve.go index bad7935..7b32f2f 100644 --- a/gopher/serve.go +++ b/gopher/serve.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "net" - "strings" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/internal" @@ -31,9 +30,8 @@ func NewServer( ) (sr.Server, error) { gs := &gopherServer{handler: handler} - if strings.IndexByte(hostname, ':') < 0 { - hostname = net.JoinHostPort(hostname, "70") - } + hostname = internal.JoinDefaultPort(hostname, "70") + address = internal.JoinDefaultPort(address, "70") var err error gs.Server, err = internal.NewServer(ctx, hostname, network, address, errLog, gs.handleConn) 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 ":<port>" 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) +} diff --git a/spartan/serve.go b/spartan/serve.go index 61199b1..a477a45 100644 --- a/spartan/serve.go +++ b/spartan/serve.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "net" - "strings" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/internal" @@ -32,9 +31,8 @@ func NewServer( ) (sr.Server, error) { ss := &spartanServer{handler: handler} - if strings.IndexByte(hostname, ':') < 0 { - hostname = net.JoinHostPort(hostname, "300") - } + hostname = internal.JoinDefaultPort(hostname, "300") + address = internal.JoinDefaultPort(address, "300") var err error ss.Server, err = internal.NewServer(ctx, hostname, network, address, errLog, ss.handleConn) |