1
0

Pass listen address as a -listen argument. Use the new code to parse a parameter.

This commit is contained in:
Alexander Andreev 2023-05-24 21:26:48 +04:00
parent ce6d81ad55
commit c43c314a78
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -9,12 +9,15 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net/netip"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
) )
var configPath *string = flag.String("conf", "config.yaml", "path to configuration file") 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 showVersion *bool = flag.Bool("v", false, "show version")
var version string var version string
@ -32,11 +35,24 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
defer func() { var network string
if typ, addr := config.SplitNetworkAddress(); typ == "unix" { if !strings.ContainsRune(*listenAddress, ':') {
os.Remove(addr) 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) logErr, err := logging.New(config.Log.Error)
if err != nil { if err != nil {
@ -83,7 +99,7 @@ func main() {
srv.GET("/f/:hash/:name", hand.Download) srv.GET("/f/:hash/:name", hand.Download)
srv.DELETE("/:hash", hand.Delete) 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) logErr.Fatalln("failed to start a server:", err)
} }