All checks were successful
Build go program / Explore-Gitea-Actions (push) Successful in 13m4s
94 lines
1.5 KiB
Go
94 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func main() {
|
|
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
|
|
}
|