diff --git a/sfu.go b/sfu.go index e3ec50d..7e57ab4 100644 --- a/sfu.go +++ b/sfu.go @@ -81,10 +81,22 @@ func main() { group.IceFilename = filepath.Join(dataDir, "ice-servers.json") go group.ReadPublicGroups() - webserver.Serve(httpAddr, dataDir) + + serverDone := make(chan struct{}) + go func() { + err := webserver.Serve(httpAddr, dataDir) + if err != nil { + log.Printf("Server: %v", err) + } + close(serverDone) + }() terminate := make(chan os.Signal, 1) signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM) - <-terminate - webserver.Shutdown() + select { + case <-terminate: + webserver.Shutdown() + case <-serverDone: + os.Exit(1) + } } diff --git a/webserver/webserver.go b/webserver/webserver.go index d64148b..1b90fe3 100644 --- a/webserver/webserver.go +++ b/webserver/webserver.go @@ -29,7 +29,7 @@ var server *http.Server var StaticRoot string -func Serve(address string, dataDir string) { +func Serve(address string, dataDir string) error { http.Handle("/", &fileHandler{http.Dir(StaticRoot)}) http.HandleFunc("/group/", groupHandler) http.HandleFunc("/recordings", @@ -61,16 +61,15 @@ func Serve(address string, dataDir string) { return true }) }) - go func() { - var err error - err = server.ListenAndServeTLS( - filepath.Join(dataDir, "cert.pem"), - filepath.Join(dataDir, "key.pem"), - ) - if err != nil && err != http.ErrServerClosed { - log.Printf("ListenAndServeTLS: %v", err) - } - }() + + err := server.ListenAndServeTLS( + filepath.Join(dataDir, "cert.pem"), + filepath.Join(dataDir, "key.pem"), + ) + if err == http.ErrServerClosed { + return nil + } + return err } func mungeHeader(w http.ResponseWriter) {