Skip to content

Commit f6d5606

Browse files
committed
fix: use net.JoinHostPort instead of custom code
Using JoinHostPort wraps IPv6 addresses in square brackets so that ListenAndServe is able to properly start the server even on a v6 interface. Use "::" (without square brackets) in the config. I added a comment to the sample config as a sort of documentation. fixes #61
1 parent 9242d00 commit f6d5606

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

config.sample.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
webserver:
2+
# For IPv6, set the listen_addr to "::"
23
listen_addr: "0.0.0.0"
34
listen_port: 8080
45
full_url: "/full-stream"

internal/web/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"net"
99
"net/http"
10+
"strconv"
1011
"time"
1112

1213
"github.com/go-chi/chi/v5"
@@ -197,7 +198,7 @@ func setupWebsocketRoutes(r *chi.Mux) {
197198
}
198199

199200
func (ws *WebServer) initServer() {
200-
addr := fmt.Sprintf("%s:%d", ws.networkIf, ws.port)
201+
addr := net.JoinHostPort(ws.networkIf, strconv.Itoa(ws.port))
201202

202203
tlsConfig := &tls.Config{
203204
MinVersion: tls.VersionTLS12,
@@ -283,8 +284,7 @@ func NewWebsocketServer(networkIf string, port int, certPath, keyPath string) *W
283284

284285
// Start initializes the webserver and starts listening for connections.
285286
func (ws *WebServer) Start() {
286-
addr := fmt.Sprintf("%s:%d", ws.networkIf, ws.port)
287-
log.Printf("Starting webserver on %s\n", addr)
287+
log.Printf("Starting webserver on %s\n", ws.server.Addr)
288288

289289
var err error
290290
if ws.keyPath != "" && ws.certPath != "" {

0 commit comments

Comments
 (0)