diff --git a/player/go.mod b/player/go.mod new file mode 100644 index 0000000..7a726f3 --- /dev/null +++ b/player/go.mod @@ -0,0 +1,3 @@ +module git.kitsu.red/restream-tools/player + +go 1.17 diff --git a/player/main.go b/player/main.go new file mode 100644 index 0000000..4494308 --- /dev/null +++ b/player/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "time" +) + +func main() { + var addr = flag.String("addr", ":8080", "http service address") + flag.Parse() + + // Map of active viewers and their last active time + var viewers = make(map[string]int64) + + http.HandleFunc("/viewcount", func(w http.ResponseWriter, r *http.Request) { + // Get the current time + var now = time.Now().Unix() + + // Get the remote address + var ip = r.RemoteAddr + + // Update the last active time + viewers[ip] = now + + // Respond with the number of viewers + fmt.Fprintf(w, "%d", len(viewers)) + }) + + // Periodically remove inactive viewers + go func() { + for { + time.Sleep(time.Second) + + // Get the current time + var now = time.Now().Unix() + + // Remove any viewers that have not been active in the last 10 seconds + for ip, lastActive := range viewers { + if now-lastActive > 10 { + delete(viewers, ip) + } + } + } + }() + + // Handle static files + http.Handle("/", http.FileServer(http.Dir("./static"))) + + // Start the server + fmt.Println("Starting server on", *addr) + err := http.ListenAndServe(*addr, nil) + if err != nil { + fmt.Println(err) + } +} diff --git a/player/index.html b/player/static/index.html similarity index 90% rename from player/index.html rename to player/static/index.html index 2116f03..f82ed91 100644 --- a/player/index.html +++ b/player/static/index.html @@ -15,6 +15,10 @@ region: 'us-east', url: 'https://us.stream.holopirates.moe', }, + { + region: 'us-east-2', + url: 'https://www.kitsu.red', + }, { region: 'sg', url: 'https://restream.kitsune.box.ca', @@ -39,6 +43,7 @@ +