Broken! Do not execute!

This commit is contained in:
Simon Martens
2024-11-11 14:03:57 +01:00
parent 28ea04f50d
commit 8be5c6a4d7
3 changed files with 152 additions and 50 deletions

View File

@@ -6,6 +6,21 @@ import (
"net/http"
"githib.com/Theodor-Springmann-Stiftung/kgpz_web/app"
"githib.com/Theodor-Springmann-Stiftung/kgpz_web/helpers"
)
type ServerState int
const (
Created ServerState = iota
Running
Restarting
ShuttingDown
ShutDown
ShuttedDown
Kill
Killing
Killed
)
// INFO: Server is a meta-package that handles the current router, which it starts in a goroutine.
@@ -16,9 +31,7 @@ import (
// - we reload all clients
// - if data validity catastrophically fails, we restart the router to map error pages.
type Server struct {
running chan bool
alive chan bool
Killed chan bool
Events helpers.EventMux[ServerState]
}
func Start(k *app.KGPZ) *Server {
@@ -26,81 +39,102 @@ func Start(k *app.KGPZ) *Server {
}
func (s *Server) Start() {
srv := &http.Server{Addr: ":8080"}
s.running = make(chan bool, 1)
s.alive = make(chan bool, 1)
s.Killed = s.killHandler(srv, s.alive)
shuttingdown := s.shutdownHandler(srv, s.running)
shutdown := s.runnerHandler(srv, shuttingdown)
s.restartHandler(shutdown)
srv := &http.Server{Addr: ":8081"}
s.killHandler(srv)
s.shutdownHandler(srv)
s.runnerHandler(srv)
s.restartHandler()
}
func (s *Server) Restart() {
s.running <- false
s.Events.Publish(ShutDown)
}
func (s *Server) Kill() {
s.alive <- false
s.Events.Publish(Kill)
}
func (s *Server) killHandler(srv *http.Server, alive chan bool) chan bool {
kill := make(chan bool, 1)
func (s *Server) restartHandler() {
shutdown := s.Events.Subscribe(1)
go func() {
<-alive
if err := srv.Shutdown(nil); err != nil {
fmt.Println("Error shutting down server")
}
kill <- true
}()
return kill
}
func (s *Server) restartHandler(shutdown chan bool) {
go func() {
<-shutdown
s.BreakUntil(shutdown, ShuttedDown)
s.Events.Publish(Restarting)
s.Start()
}()
}
func (s *Server) runnerHandler(srv *http.Server, shuttingdown chan bool) chan bool {
shutdown := make(chan bool, 1)
func (s *Server) runnerHandler(srv *http.Server) {
shutttingdown := s.Events.Subscribe(1)
go func() {
s.Events.Publish(Running)
// EXAMPLE:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hello world\n")
})
srv.Handler = mux
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
fmt.Println("Error starting server")
return
}
<-shuttingdown
shutdown <- true
loop:
for {
msg := <-shutttingdown
if msg == ShuttingDown {
s.Events.Publish(ShuttedDown)
break loop
}
if msg == Killing {
s.Events.Publish(Killed)
break loop
}
}
}()
return shutdown
}
func (s *Server) shutdownHandler(srv *http.Server, running chan bool) chan bool {
shuttingdown := make(chan bool, 1)
func (s *Server) shutdownHandler(srv *http.Server) {
shutdown := s.Events.Subscribe(1)
go func() {
<-running
s.BreakUntil(shutdown, ShutDown)
fmt.Println("Shutting down server")
if err := srv.Shutdown(nil); err != nil {
fmt.Println("Error shutting down server")
}
shuttingdown <- true
s.Events.Publish(ShuttingDown)
}()
return shuttingdown
}
func (s *Server) Shutdown() {
func (s *Server) killHandler(srv *http.Server) {
kill := s.Events.Subscribe(1)
go func() {
s.BreakUntil(kill, Kill)
fmt.Println("Killing server")
if err := srv.Shutdown(nil); err != nil {
fmt.Println("Error shutting down server")
}
s.Events.Publish(Killing)
}()
}
func (s *Server) BreakUntil(c chan ServerState, state ServerState) {
loop:
for {
msg := <-c
if msg == state {
break loop
}
}
s.Events.Unsubscribe(c)
}