diff --git a/galene.go b/galene.go index 86745d0..09f0862 100644 --- a/galene.go +++ b/galene.go @@ -126,14 +126,10 @@ func main() { ice.Update() defer turnserver.Stop() - serverDone := make(chan struct{}) - go func() { - err := webserver.Serve(httpAddr, group.DataDirectory) - if err != nil { - log.Printf("Server: %v", err) - } - close(serverDone) - }() + err = webserver.Serve(httpAddr, group.DataDirectory) + if err != nil { + log.Fatalf("Server: %v", err) + } terminate := make(chan os.Signal, 1) signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM) @@ -158,8 +154,6 @@ func main() { case <-terminate: webserver.Shutdown() return - case <-serverDone: - os.Exit(1) } } } diff --git a/webserver/api_test.go b/webserver/api_test.go index f7aae5a..70ee9fb 100644 --- a/webserver/api_test.go +++ b/webserver/api_test.go @@ -7,7 +7,6 @@ import ( "os" "strings" "sync" - "time" "encoding/json" "net/http" @@ -19,13 +18,10 @@ import ( var setupOnce = sync.OnceFunc(func() { Insecure = true - go func() { - err := Serve("localhost:1234", "") - if err != nil { - panic("could not start server") - } - }() - time.Sleep(100 * time.Millisecond) + err := Serve("localhost:1234", "") + if err != nil { + panic("could not start server") + } }) func setupTest(dir, datadir string) error { diff --git a/webserver/webserver.go b/webserver/webserver.go index 06964fc..7e7ace7 100644 --- a/webserver/webserver.go +++ b/webserver/webserver.go @@ -17,7 +17,6 @@ import ( "path/filepath" "sort" "strings" - "sync/atomic" "time" "github.com/gorilla/websocket" @@ -28,7 +27,7 @@ import ( "github.com/jech/galene/rtpconn" ) -var server atomic.Value +var server *http.Server var StaticRoot string @@ -67,7 +66,7 @@ func Serve(address string, dataDir string) error { group.Shutdown("server is shutting down") }) - server.Store(s) + server = s proto := "tcp" if strings.HasPrefix(address, "/") { @@ -78,18 +77,15 @@ func Serve(address string, dataDir string) error { if err != nil { return err } - defer listener.Close() - - if !Insecure { - err = s.ServeTLS(listener, "", "") - } else { - err = s.Serve(listener) - } - - if err == http.ErrServerClosed { - return nil - } - return err + go func() { + defer listener.Close() + if !Insecure { + err = s.ServeTLS(listener, "", "") + } else { + err = s.Serve(listener) + } + }() + return nil } func cspHeader(w http.ResponseWriter, connect string) { @@ -755,12 +751,11 @@ func serveGroupRecordings(w http.ResponseWriter, r *http.Request, f *os.File, gr } func Shutdown() { - v := server.Load() - if v == nil { - return + if server == nil { + log.Printf("Shutting down nonexistent server") } - s := v.(*http.Server) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() - s.Shutdown(ctx) + server.Shutdown(ctx) + server = nil }