update some...
All checks were successful
Build go program / Explore-Gitea-Actions (push) Successful in 13m4s

This commit is contained in:
Adrian Zürcher
2025-12-14 21:54:13 +01:00
parent 165fe1b9e5
commit f786edfb90
4 changed files with 198 additions and 2 deletions

90
main.go
View File

@@ -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
}