diff options
Diffstat (limited to 'send_over_http.go')
-rw-r--r-- | send_over_http.go | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/send_over_http.go b/send_over_http.go index c34145b..98b7e6e 100644 --- a/send_over_http.go +++ b/send_over_http.go @@ -6,10 +6,15 @@ import ( "log" "net" "net/http" + "net/url" "os" "path/filepath" + "strconv" ) +// port is the IP port address to listen on. +const port = 8081 + func preferredIP() (*net.IP, error) { ips, err := net.InterfaceAddrs() if err != nil { @@ -76,20 +81,20 @@ func (s *Server) Start() error { return err } - prot := "http" - port := "8081" - - url := fmt.Sprintf("%s://%s:%s", prot, ip, port) + u := url.URL{ + Scheme: "http", + Host: fmt.Sprintf("%s:%d", ip, port), + } if stat, _ := os.Stat(s.filepath); !stat.IsDir() { - url += "/" + filepath.Base(s.filepath) + u.Path = filepath.Base(s.filepath) } - qrCodeShower := NewTerminalQRShower(url) + qrCodeShower := NewTerminalQRShower(u.String()) go s.mr.Run(qrCodeShower) httpRunner := NewRunner() httpSrv := &http.Server{ - Addr: ":" + port, + Addr: ":" + strconv.Itoa(port), } var d http.FileSystem = http.Dir(s.filepath) @@ -100,7 +105,7 @@ func (s *Server) Start() error { httpSrv.Handler = http.FileServer(d) httpRunner.OnStart = func() error { - l, err := net.Listen("tcp4", ip.String()+":"+port) + l, err := net.Listen("tcp4", u.Host) if err != nil { log.Fatal(err) } |