Add Basic Websocket Server/Worker Connection
This commit is contained in:
parent
01e34936e9
commit
9ac9ed8fe2
9 changed files with 218 additions and 7 deletions
55
server/server.go
Normal file
55
server/server.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/config"
|
||||
)
|
||||
|
||||
var conf config.Config
|
||||
|
||||
func Start(_conf config.Config) {
|
||||
conf = _conf
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/worker", handleWorkerWebsocket)
|
||||
mux.HandleFunc("/", handleIndex)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: conf.Server.Address,
|
||||
Handler: mux,
|
||||
ReadTimeout: 1 * time.Second,
|
||||
WriteTimeout: 1 * time.Second,
|
||||
IdleTimeout: 30 * time.Second,
|
||||
ReadHeaderTimeout: 2 * time.Second,
|
||||
}
|
||||
|
||||
serverClose := make(chan bool)
|
||||
go func() {
|
||||
slog.Info("Listening...", "Address", conf.Server.Address)
|
||||
err := server.ListenAndServe()
|
||||
if err != http.ErrServerClosed {
|
||||
slog.Error("Listen Failed", "err", err)
|
||||
} else {
|
||||
slog.Info("Server Closed")
|
||||
}
|
||||
serverClose <- true
|
||||
}()
|
||||
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, os.Interrupt)
|
||||
select {
|
||||
case <-serverClose:
|
||||
slog.Info("Exiting due to Listen Failure")
|
||||
case sig := <-sigs:
|
||||
slog.Info("Stopping Server...", "signal", sig)
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
server.Shutdown(stopCtx)
|
||||
cancel()
|
||||
slog.Info("Done")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue