From a8e31036363c7aef52e60b4b7939f283400ca3fe Mon Sep 17 00:00:00 2001 From: six-ddc Date: Sat, 26 Jun 2021 21:40:28 +0800 Subject: [PATCH] Update listen address --- charts.go | 16 +++++----------- main.go | 15 +++++++++++---- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/charts.go b/charts.go index 41f9aeb..55c2d2a 100644 --- a/charts.go +++ b/charts.go @@ -140,21 +140,15 @@ type Metrics struct { } type Charts struct { - listenAddr string - page *components.Page - ln net.Listener - dataFunc func() *ChartsReport + page *components.Page + ln net.Listener + dataFunc func() *ChartsReport } -func NewCharts(listenAddr string, dataFunc func() *ChartsReport, desc string) (*Charts, error) { +func NewCharts(ln net.Listener, dataFunc func() *ChartsReport, desc string) (*Charts, error) { templates.PageTpl = fmt.Sprintf(PageTpl, desc) - ln, err := net.Listen("tcp4", listenAddr) - if err != nil { - return nil, err - } - - c := &Charts{listenAddr: listenAddr, ln: ln, dataFunc: dataFunc} + c := &Charts{ln: ln, dataFunc: dataFunc} c.page = components.NewPage() c.page.PageTitle = "plow" c.page.AssetsHost = assertsPath diff --git a/main.go b/main.go index a49b457..6442ab5 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/ioutil" + "net" "os" "strings" @@ -23,7 +24,7 @@ var ( host = kingpin.Flag("host", "Host header").String() contentType = kingpin.Flag("content", "Content-Type header").Short('T').String() - chartsListenAddr = kingpin.Flag("listen", "Listen addr to serve Web UI").Default("0.0.0.0:18888").String() + chartsListenAddr = kingpin.Flag("listen", "Listen addr to serve Web UI").Default(":18888").String() timeout = kingpin.Flag("timeout", "Timeout for each http request").PlaceHolder("DURATION").Duration() dialTimeout = kingpin.Flag("dial-timeout", "Timeout for dial addr").PlaceHolder("DURATION").Duration() reqWriteTimeout = kingpin.Flag("req-timeout", "Timeout for full request writing").PlaceHolder("DURATION").Duration() @@ -102,8 +103,14 @@ Example: desc += fmt.Sprintf(" using %d connection(s).", *concurrency) fmt.Println(desc) + var ln net.Listener if *chartsListenAddr != "" { - fmt.Printf("@ Real-time charts is listening on http://%s\n", *chartsListenAddr) + ln, err = net.Listen("tcp", *chartsListenAddr) + if err != nil { + errAndExit(err.Error()) + return + } + fmt.Printf("@ Real-time charts is listening on http://%s\n", ln.Addr().String()) } fmt.Printf("\n") @@ -117,8 +124,8 @@ Example: report := NewStreamReport() go report.Collect(requester.RecordChan()) - if *chartsListenAddr != "" { - charts, err := NewCharts(*chartsListenAddr, report.Charts, desc) + if ln != nil { + charts, err := NewCharts(ln, report.Charts, desc) if err != nil { errAndExit(err.Error()) return