morffix/server/server.go

55 lines
1.2 KiB
Go

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")
}
}