start: make http server's listening host/port compatible with IPv6 (#14)

previously, a command like this to listen on IPv6 loopback:

    HOST=::1 PORT=7447 go run ./basic/

would exit immediately because ::1:7447 is an invalid address.

IPv6 addresses contain columns, so a simple host + port concatenation
doesn't work. net.JoinHostPort is a function to do exactly that.
This commit is contained in:
alex 2022-12-24 22:53:05 +01:00 committed by GitHub
parent 642710fda8
commit 932a9b62a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package relayer
import (
"net"
"fmt"
"net/http"
"os"
@ -72,7 +73,7 @@ func StartConf(s Settings, relay Relay) error {
// start http server
srv := &http.Server{
Handler: cors.Default().Handler(Router),
Addr: s.Host + ":" + s.Port,
Addr: net.JoinHostPort(s.Host, s.Port),
WriteTimeout: 2 * time.Second,
ReadTimeout: 2 * time.Second,
IdleTimeout: 30 * time.Second,