From 1a2b6dba71d18bc972a6705c2244c5d4ee5020b1 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Mon, 9 Dec 2024 14:08:07 -0500 Subject: [PATCH] Update server.go --- wap/pkg/server/server.go | 41 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/wap/pkg/server/server.go b/wap/pkg/server/server.go index 03d8d6c..5641b85 100644 --- a/wap/pkg/server/server.go +++ b/wap/pkg/server/server.go @@ -768,16 +768,18 @@ func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip mux.HandleFunc("/account/id", accountIdHandler) mux.HandleFunc("/account/seed", accountSeedHandler) - listenAddr := "" + mc := &multiCloser{ + listeners: []io.Closer{}, + } + // Try first listener + listenAddr := "" if ip == "" { ip = config.IPADDRESS } - if port == "" { port = config.API_PORT } - listenAddr = ip + ":" + port ln, err := net.Listen("tcp", listenAddr) @@ -786,11 +788,6 @@ func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip ip, err = getIPFromSpecificNetwork(ctx, config.HOTSPOT_SSID) if err != nil { log.Errorw("Failed to use IP of hotspot", "err", err) - /*ip, err = getNonLoopbackIP() - if err != nil { - log.Errorw("Failed to get non-loopback IP address for serve", "err", err) - ip = "0.0.0.0" - }*/ ip = "0.0.0.0" } listenAddr = ip + ":" + port @@ -799,34 +796,32 @@ func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip if err != nil { listenAddr = "0.0.0.0:" + port ln, err = net.Listen("tcp", listenAddr) - if err != nil { - log.Errorw("Listen could not initialize for serve", "err", err) - } } } - log.Info("Starting server at " + listenAddr) - go func() { - if err := http.Serve(ln, mux); err != nil { - log.Errorw("Serve could not initialize", "err", err) - } - }() - - mc := &multiCloser{ - listeners: []io.Closer{ln}, + if err == nil { + mc.listeners = append(mc.listeners, ln) + log.Info("Starting server at " + listenAddr) + go func() { + if err := http.Serve(ln, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") { + log.Errorw("Serve could not initialize", "err", err) + } + }() } + // Try second listener (localhost) ln1, err1 := net.Listen("tcp", "127.0.0.1:"+port) - if err1 != nil { - log.Errorw("Failed to use 127.0.0.1 for serve", "err", err1) - } else { + if err1 == nil { mc.listeners = append(mc.listeners, ln1) go func() { if err := http.Serve(ln1, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") { log.Errorw("Serve could not initialize on 127.0.0.1", "err", err) } }() + } else { + log.Errorw("Failed to use 127.0.0.1 for serve", "err", err1) } + // Return even if no listeners were successful return mc }