From c43c314a78068d23eb79e14a825786ecf8d240df Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Wed, 24 May 2023 21:26:48 +0400 Subject: [PATCH] Pass listen address as a -listen argument. Use the new code to parse a parameter. --- cmd/dwelling-upload/main.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/dwelling-upload/main.go b/cmd/dwelling-upload/main.go index 99b0198..482d61b 100644 --- a/cmd/dwelling-upload/main.go +++ b/cmd/dwelling-upload/main.go @@ -9,12 +9,15 @@ import ( "flag" "fmt" "log" + "net/netip" "os" "os/signal" + "strings" "syscall" ) var configPath *string = flag.String("conf", "config.yaml", "path to configuration file") +var listenAddress *string = flag.String("listen", "/var/run/dwelling-upload/sock", "listen address (ip:port|unix_path)") var showVersion *bool = flag.Bool("v", false, "show version") var version string @@ -32,11 +35,24 @@ func main() { log.Fatalln(err) } - defer func() { - if typ, addr := config.SplitNetworkAddress(); typ == "unix" { - os.Remove(addr) + var network string + if !strings.ContainsRune(*listenAddress, ':') { + network = "unix" + defer os.Remove(*listenAddress) + } else { + ap, err := netip.ParseAddrPort(*listenAddress) + if err != nil { + log.Fatalln(err) + } else if !ap.IsValid() { + log.Fatalln(*listenAddress, "is not valid") } - }() + + if ap.Addr().Is4() { + network = "tcp4" + } else if ap.Addr().Is6() { + network = "tcp6" + } + } logErr, err := logging.New(config.Log.Error) if err != nil { @@ -83,7 +99,7 @@ func main() { srv.GET("/f/:hash/:name", hand.Download) srv.DELETE("/:hash", hand.Delete) - if err := srv.Start(config.SplitNetworkAddress()); err != nil { + if err := srv.Start(network, *listenAddress); err != nil { logErr.Fatalln("failed to start a server:", err) }