split web server into a listen and serve call

This commit is contained in:
sawka 2024-07-09 13:47:39 -07:00
parent f3b649aef1
commit fa3a670603
3 changed files with 22 additions and 14 deletions

View File

@ -9,9 +9,9 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strconv"
"runtime" "runtime"
"strconv"
"sync" "sync"
"syscall" "syscall"
"time" "time"
@ -119,10 +119,12 @@ func main() {
installShutdownSignalHandlers() installShutdownSignalHandlers()
go stdinReadWatch() go stdinReadWatch()
configWatcher() configWatcher()
go web.RunWebSocketServer() go web.RunWebSocketServer()
webListener, err := web.MakeTCPListener()
if err != nil {
log.Printf("error creating web listener: %v\n", err)
}
go func() { go func() {
time.Sleep(30 * time.Millisecond)
pidStr := os.Getenv(ReadySignalPidVarName) pidStr := os.Getenv(ReadySignalPidVarName)
if pidStr != "" { if pidStr != "" {
_, err := strconv.Atoi(pidStr) _, err := strconv.Atoi(pidStr)
@ -132,6 +134,6 @@ func main() {
} }
} }
}() }()
web.RunWebServer() // blocking web.RunWebServer(webListener) // blocking
runtime.KeepAlive(waveLock) runtime.KeepAlive(waveLock)
} }

View File

@ -142,8 +142,6 @@ func (w *Watcher) sendInitialValues() error {
} }
w.settingsData = message.Update w.settingsData = message.Update
log.Printf("watcher: initial values: %s -> %v", filename, content)
return nil return nil
} }

View File

@ -10,6 +10,7 @@ import (
"io" "io"
"io/fs" "io/fs"
"log" "log"
"net"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
"strconv" "strconv"
@ -211,29 +212,36 @@ func WebFnWrap(opts WebFnOpts, fn WebFnType) WebFnType {
} }
} }
func MakeTCPListener() (net.Listener, error) {
serverAddr := MainServerAddr
if wavebase.IsDevMode() {
serverAddr = MainServerDevAddr
}
rtn, err := net.Listen("tcp", serverAddr)
if err != nil {
return nil, fmt.Errorf("error creating listener at %v: %v", serverAddr, err)
}
log.Printf("Server listening on %s\n", serverAddr)
return rtn, nil
}
// blocking // blocking
// TODO: create listener separately and use http.Serve, so we can signal SIGUSR1 in a better way func RunWebServer(listener net.Listener) {
func RunWebServer() {
gr := mux.NewRouter() gr := mux.NewRouter()
gr.HandleFunc("/wave/stream-file", WebFnWrap(WebFnOpts{AllowCaching: true}, handleStreamFile)) gr.HandleFunc("/wave/stream-file", WebFnWrap(WebFnOpts{AllowCaching: true}, handleStreamFile))
gr.HandleFunc("/wave/file", WebFnWrap(WebFnOpts{AllowCaching: false}, handleWaveFile)) gr.HandleFunc("/wave/file", WebFnWrap(WebFnOpts{AllowCaching: false}, handleWaveFile))
gr.HandleFunc("/wave/service", WebFnWrap(WebFnOpts{JsonErrors: true}, handleService)) gr.HandleFunc("/wave/service", WebFnWrap(WebFnOpts{JsonErrors: true}, handleService))
serverAddr := MainServerAddr
var allowedOrigins handlers.CORSOption var allowedOrigins handlers.CORSOption
if wavebase.IsDevMode() { if wavebase.IsDevMode() {
log.Println("isDevMode")
serverAddr = MainServerDevAddr
allowedOrigins = handlers.AllowedOrigins([]string{"*"}) allowedOrigins = handlers.AllowedOrigins([]string{"*"})
} }
server := &http.Server{ server := &http.Server{
Addr: serverAddr,
ReadTimeout: HttpReadTimeout, ReadTimeout: HttpReadTimeout,
WriteTimeout: HttpWriteTimeout, WriteTimeout: HttpWriteTimeout,
MaxHeaderBytes: HttpMaxHeaderBytes, MaxHeaderBytes: HttpMaxHeaderBytes,
Handler: handlers.CORS(allowedOrigins)(http.TimeoutHandler(gr, HttpTimeoutDuration, "Timeout")), Handler: handlers.CORS(allowedOrigins)(http.TimeoutHandler(gr, HttpTimeoutDuration, "Timeout")),
} }
log.Printf("Running main server on %s\n", serverAddr) err := server.Serve(listener)
err := server.ListenAndServe()
if err != nil { if err != nil {
log.Printf("ERROR: %v\n", err) log.Printf("ERROR: %v\n", err)
} }