1
0

Code for determining of network type was moved to HttpServer's Start() func.

This commit is contained in:
Alexander Andreev 2023-08-06 01:26:41 +04:00
parent 72d3104150
commit 532e8d9da6
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 21 additions and 19 deletions

View File

@ -40,23 +40,6 @@ func main() {
return return
} }
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)
}
if ap.Addr().Is4() {
network = "tcp4"
} else if ap.Addr().Is6() {
network = "tcp6"
}
}
watcha, err := watcher.NewInotifyWatcher() watcha, err := watcher.NewInotifyWatcher()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
@ -116,7 +99,7 @@ func main() {
} }
}() }()
if err := srv.Start(network, *listenAddress); err != nil { if err := srv.Start(*listenAddress); err != nil {
log.Fatalln("failed to start a server:", err) log.Fatalln("failed to start a server:", err)
} }

View File

@ -5,7 +5,9 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/netip"
"os" "os"
"strings"
"time" "time"
) )
@ -20,7 +22,24 @@ func NewHttpServer(r http.Handler) *HttpServer {
Handler: r}} Handler: r}}
} }
func (s *HttpServer) Start(network, address string) error { func (s *HttpServer) Start(address string) error {
var network string
if !strings.ContainsRune(address, ':') {
network = "unix"
defer os.Remove(address)
} else {
ap, err := netip.ParseAddrPort(address)
if err != nil {
log.Fatalln(err)
}
if ap.Addr().Is4() {
network = "tcp4"
} else if ap.Addr().Is6() {
network = "tcp6"
}
}
listener, err := net.Listen(network, address) listener, err := net.Listen(network, address)
if err != nil { if err != nil {
return err return err