From 4d7d563c3031cc19ad5ee49d2d3e0f737ca85a0c Mon Sep 17 00:00:00 2001 From: hytracer Date: Wed, 22 Nov 2023 15:00:55 +0100 Subject: [PATCH] feat: implement user counting and return on /online --- common/stats.go | 12 ++++++++++++ gpcm/main.go | 3 +++ nas/route.go | 6 +++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 common/stats.go diff --git a/common/stats.go b/common/stats.go new file mode 100644 index 0000000..23ecfb9 --- /dev/null +++ b/common/stats.go @@ -0,0 +1,12 @@ +package common + +var OnlineUsers int + +func init(){ + OnlineUsers = 0 +} + +func OnlineStatUpdate(t int) { + OnlineUsers += t +} + diff --git a/gpcm/main.go b/gpcm/main.go index 9c12f5e..6508e60 100644 --- a/gpcm/main.go +++ b/gpcm/main.go @@ -122,6 +122,7 @@ func handleRequest(conn net.Conn) { conn.Write([]byte(payload)) logging.Notice(session.ModuleName, "Connection established from", conn.RemoteAddr()) + common.OnlineStatUpdate(1) // Here we go into the listening loop for { @@ -132,10 +133,12 @@ func handleRequest(conn net.Conn) { if errors.Is(err, io.EOF) { // Client closed connection, terminate. logging.Notice(session.ModuleName, "Client closed connection") + common.OnlineStatUpdate(-1) return } logging.Error(session.ModuleName, "Connection lost") + common.OnlineStatUpdate(-1) return } diff --git a/nas/route.go b/nas/route.go index 094c77e..9a8a91c 100644 --- a/nas/route.go +++ b/nas/route.go @@ -10,6 +10,7 @@ import ( "strings" "wwfc/logging" "wwfc/sake" + "wwfc/common" ) type Route struct { @@ -75,10 +76,9 @@ func (route *Route) Handle() http.Handler { // Check for /online if strings.HasPrefix(r.URL.String(), "/online") { - logging.Notice("test") w.Header().Set("Content-Type", "text/plain") - w.Header().Set("Content-Length", strconv.Itoa(len("Success"))) - w.Write([]byte("Success")) + w.Header().Set("Content-Length", strconv.Itoa(len(strconv.Itoa(common.OnlineUsers)))) + w.Write([]byte(strconv.Itoa(common.OnlineUsers))) return }