From f786edfb90683ccb225ed50aeed38b6ce3c336dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Z=C3=BCrcher?= Date: Sun, 14 Dec 2025 21:54:13 +0100 Subject: [PATCH] update some... --- go.mod | 4 ++ go.sum | 4 ++ main.go | 90 ++++++++++++++++++++++++++++++++++++++++++++- websocket.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 go.sum create mode 100644 websocket.go diff --git a/go.mod b/go.mod index 445e9e6..e3171ba 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module test go 1.24.5 + +require github.com/gorilla/mux v1.8.1 + +require github.com/gorilla/websocket v1.5.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7ed87b7 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= diff --git a/main.go b/main.go index 056d75a..16cb63c 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,93 @@ package main -import "fmt" +import ( + "flag" + "fmt" + "net/http" + "time" + + "github.com/gorilla/mux" +) func main() { - fmt.Println("hello from go") + port := flag.Uint("port", 9401, "listening port") + flag.Parse() + + router := mux.NewRouter() + + router.HandleFunc("/start", start) + router.HandleFunc("/stop", stop) + router.HandleFunc("/status", state) + router.HandleFunc("/ws", websocketHandler) + + http.Handle("/", router) + http.ListenAndServe(fmt.Sprintf(":%d", *port), nil) +} + +var Prg Program + +func start(w http.ResponseWriter, r *http.Request) { + if Prg.Run { + w.Write([]byte("Programm already running")) + setState("Programm already running") + + return + } + Prg.start() + w.Write([]byte("Program started")) + setState("Programm started") + +} + +func stop(w http.ResponseWriter, r *http.Request) { + if !Prg.Run { + w.Write([]byte("Programm already stopped")) + setState("Programm already stopped") + + return + } + + Prg.stop() + w.Write([]byte("Program stopped")) + setState("Programm stopped") + +} + +func state(w http.ResponseWriter, r *http.Request) { + if !Prg.Run { + w.Write([]byte("Programm stopped")) + setState("Programm stopped") + return + } + + w.Write([]byte(Prg.State)) +} + +type Program struct { + Run bool + State string +} + +func (p *Program) start() { + if p.Run { + return + } + + go func() { + p.Run = true + counter := 0 + for p.Run { + counter++ + if counter > 100 { + counter = 0 + } + p.State = fmt.Sprintf("Hello, Go! :%d", counter) + setState(fmt.Sprintf("Hello, Go! :%d", counter)) + time.Sleep(time.Second) + } + }() +} + +func (p *Program) stop() { + p.Run = false } diff --git a/websocket.go b/websocket.go new file mode 100644 index 0000000..15733f0 --- /dev/null +++ b/websocket.go @@ -0,0 +1,102 @@ +package main + +import ( + "log" + "net/http" + "sync" + "time" + + "github.com/gorilla/websocket" +) + +// WebSocket upgrader +var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + // Allow all origins (CORS) + CheckOrigin: func(r *http.Request) bool { + return true + }, +} + +var ( + clients = make(map[*websocket.Conn]bool) + clientsMu sync.Mutex + + appState = "idle" + appStateMu sync.Mutex +) + +func websocketHandler(w http.ResponseWriter, r *http.Request) { + conn, err := upgrader.Upgrade(w, r, nil) + if err != nil { + log.Println("WebSocket upgrade error:", err) + return + } + defer conn.Close() + + // Register client + clientsMu.Lock() + clients[conn] = true + clientsMu.Unlock() + + log.Println("Client connected") + + // Optionally send initial state + sendStateToClient(conn) + + for { + _, _, err := conn.ReadMessage() + if err != nil { + break + } + // (Ignore incoming messages — or handle if needed) + } + + // Remove client on disconnect + clientsMu.Lock() + delete(clients, conn) + clientsMu.Unlock() + + log.Println("Client disconnected") +} + +func setState(newState string) { + appStateMu.Lock() + defer appStateMu.Unlock() + + if appState != newState { + appState = newState + //log.Printf("State changed to: %s", newState) + broadcastState() + } +} + +func broadcastState() { + clientsMu.Lock() + defer clientsMu.Unlock() + + for conn := range clients { + err := conn.WriteJSON(map[string]string{ + "type": "state_update", + "state": appState, + "time": time.Now().Format(time.RFC3339), + }) + if err != nil { + log.Println("Error sending to client, removing:", err) + conn.Close() + delete(clients, conn) + } + } +} + +func sendStateToClient(conn *websocket.Conn) { + appStateMu.Lock() + defer appStateMu.Unlock() + + conn.WriteJSON(map[string]string{ + "type": "state_update", + "state": appState, + "time": time.Now().Format(time.RFC3339), + }) +}