clean HTTP sockets on shutdown

This commit is contained in:
boyska 2018-11-10 17:36:17 +01:00
parent 917e457af0
commit c70e28ff27

View file

@ -58,6 +58,7 @@ func main() {
go hub.Run()
setupHTTP(hub)
httpServer := http.Server{Handler: nil}
if *querySocket != "" {
fmt.Printf("Binding address `%s` [http]\n", *querySocket)
unixListener, err := net.Listen("unix", *querySocket)
@ -65,16 +66,17 @@ func main() {
fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err)
return
}
defer cleanSocket(*querySocket)
go func() {
err := http.Serve(unixListener, nil)
if err != nil {
if err := httpServer.Serve(unixListener); err != nil {
fmt.Fprintln(os.Stderr, "error binding:", err)
}
}()
} else {
httpServer.Addr = *queryAddr
fmt.Printf("Binding address `%s` [http]\n", *queryAddr)
go func() {
err := http.ListenAndServe(*queryAddr, nil)
err := httpServer.ListenAndServe()
if err != nil {
fmt.Fprintln(os.Stderr, "error binding:", err)
}
@ -85,6 +87,9 @@ func main() {
select {
case <-interrupt:
server.Kill()
if err := httpServer.Close(); err != nil {
fmt.Fprintln(os.Stderr, "Error closing http server:", err)
}
return
default:
}