From 7666b8e9bb947a2b9afad1be704905771f032317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=B0?= Date: Mon, 9 May 2022 14:30:48 +0800 Subject: [PATCH] feat: dashboard supports random port MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 柳丰 --- cmd/webserver.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/webserver.go b/cmd/webserver.go index 867a2e2..c2178fa 100644 --- a/cmd/webserver.go +++ b/cmd/webserver.go @@ -19,6 +19,7 @@ import ( "fmt" "io/ioutil" "log" + "net" "net/http" "os" "path/filepath" @@ -105,14 +106,18 @@ func RunWebServer(port int) { spa := spaHandler{staticPath: "web/dist", indexPath: "index.html"} r.PathPrefix("/").Handler(spa) + listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%v", port)) + if err != nil { + log.Fatal(err) + } + port = listener.Addr().(*net.TCPAddr).Port srv := &http.Server{ Handler: r, - Addr: fmt.Sprintf("0.0.0.0:%v", port), WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } fmt.Printf("Dapr Dashboard running on http://localhost:%v\n", port) - log.Fatal(srv.ListenAndServe()) + log.Fatal(srv.Serve(listener)) } // ServeHTTP inspects the URL path to locate a file within the static dir