From f5cb2ff3289b01339fcef55d20485b8812857b35 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Fri, 18 Sep 2020 14:14:26 +0200 Subject: [PATCH] Rework synchronisation between webserver and main. We now exit with an error code if the webserver couldn't start. --- sfu.go | 18 +++++++++++++++--- webserver/webserver.go | 21 ++++++++++----------- 2 files changed, 25 insertions(+), 14 deletions(-) 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) {