From 06c03762a0578ea49df58e5fdc0c4bf76175e880 Mon Sep 17 00:00:00 2001 From: hytracer Date: Wed, 22 Nov 2023 14:20:59 +0100 Subject: [PATCH 1/3] feat: add /online endpoint --- nas/route.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nas/route.go b/nas/route.go index 1a3a3bf..094c77e 100644 --- a/nas/route.go +++ b/nas/route.go @@ -73,6 +73,15 @@ func (route *Route) Handle() http.Handler { return } + // 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")) + return + } + err := r.ParseForm() if err != nil { logging.Error(moduleName, "Failed to parse form") From 4d7d563c3031cc19ad5ee49d2d3e0f737ca85a0c Mon Sep 17 00:00:00 2001 From: hytracer Date: Wed, 22 Nov 2023 15:00:55 +0100 Subject: [PATCH 2/3] 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 } From 8ee2db155ae3156c9aab82298e345a9741e86477 Mon Sep 17 00:00:00 2001 From: hytracer Date: Wed, 22 Nov 2023 15:08:45 +0100 Subject: [PATCH 3/3] chore: refactor online request handler in separate files --- nas/online.go | 13 +++++++++++++ nas/route.go | 5 +---- 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 nas/online.go diff --git a/nas/online.go b/nas/online.go new file mode 100644 index 0000000..0560aa4 --- /dev/null +++ b/nas/online.go @@ -0,0 +1,13 @@ +package nas + +import ( + "strconv" + "wwfc/common" + "net/http" +) + +func returnOnlineStats(w http.ResponseWriter) { + w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Length", strconv.Itoa(len(strconv.Itoa(common.OnlineUsers)))) + w.Write([]byte(strconv.Itoa(common.OnlineUsers))) +} diff --git a/nas/route.go b/nas/route.go index 9a8a91c..b693278 100644 --- a/nas/route.go +++ b/nas/route.go @@ -10,7 +10,6 @@ import ( "strings" "wwfc/logging" "wwfc/sake" - "wwfc/common" ) type Route struct { @@ -76,9 +75,7 @@ func (route *Route) Handle() http.Handler { // Check for /online if strings.HasPrefix(r.URL.String(), "/online") { - w.Header().Set("Content-Type", "text/plain") - w.Header().Set("Content-Length", strconv.Itoa(len(strconv.Itoa(common.OnlineUsers)))) - w.Write([]byte(strconv.Itoa(common.OnlineUsers))) + returnOnlineStats(w) return }