1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-09 18:25:58 +01:00

Simplify server startup.

We now create the server and the listener in the calling thread,
and only create a new goroutine before calling server.Serve.
This commit is contained in:
Juliusz Chroboczek 2024-04-14 02:12:46 +02:00
parent 8fb1c1556e
commit 31f89163a2
3 changed files with 23 additions and 38 deletions

View file

@ -126,14 +126,10 @@ func main() {
ice.Update() ice.Update()
defer turnserver.Stop() defer turnserver.Stop()
serverDone := make(chan struct{}) err = webserver.Serve(httpAddr, group.DataDirectory)
go func() { if err != nil {
err := webserver.Serve(httpAddr, group.DataDirectory) log.Fatalf("Server: %v", err)
if err != nil { }
log.Printf("Server: %v", err)
}
close(serverDone)
}()
terminate := make(chan os.Signal, 1) terminate := make(chan os.Signal, 1)
signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM) signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM)
@ -158,8 +154,6 @@ func main() {
case <-terminate: case <-terminate:
webserver.Shutdown() webserver.Shutdown()
return return
case <-serverDone:
os.Exit(1)
} }
} }
} }

View file

@ -7,7 +7,6 @@ import (
"os" "os"
"strings" "strings"
"sync" "sync"
"time"
"encoding/json" "encoding/json"
"net/http" "net/http"
@ -19,13 +18,10 @@ import (
var setupOnce = sync.OnceFunc(func() { var setupOnce = sync.OnceFunc(func() {
Insecure = true Insecure = true
go func() { err := Serve("localhost:1234", "")
err := Serve("localhost:1234", "") if err != nil {
if err != nil { panic("could not start server")
panic("could not start server") }
}
}()
time.Sleep(100 * time.Millisecond)
}) })
func setupTest(dir, datadir string) error { func setupTest(dir, datadir string) error {

View file

@ -17,7 +17,6 @@ import (
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"sync/atomic"
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -28,7 +27,7 @@ import (
"github.com/jech/galene/rtpconn" "github.com/jech/galene/rtpconn"
) )
var server atomic.Value var server *http.Server
var StaticRoot string var StaticRoot string
@ -67,7 +66,7 @@ func Serve(address string, dataDir string) error {
group.Shutdown("server is shutting down") group.Shutdown("server is shutting down")
}) })
server.Store(s) server = s
proto := "tcp" proto := "tcp"
if strings.HasPrefix(address, "/") { if strings.HasPrefix(address, "/") {
@ -78,18 +77,15 @@ func Serve(address string, dataDir string) error {
if err != nil { if err != nil {
return err return err
} }
defer listener.Close() go func() {
defer listener.Close()
if !Insecure { if !Insecure {
err = s.ServeTLS(listener, "", "") err = s.ServeTLS(listener, "", "")
} else { } else {
err = s.Serve(listener) err = s.Serve(listener)
} }
}()
if err == http.ErrServerClosed { return nil
return nil
}
return err
} }
func cspHeader(w http.ResponseWriter, connect string) { 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() { func Shutdown() {
v := server.Load() if server == nil {
if v == nil { log.Printf("Shutting down nonexistent server")
return
} }
s := v.(*http.Server)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel() defer cancel()
s.Shutdown(ctx) server.Shutdown(ctx)
server = nil
} }