feat: implement user counting and return on /online

This commit is contained in:
hytracer
2023-11-22 15:00:55 +01:00
parent 06c03762a0
commit 4d7d563c30
3 changed files with 18 additions and 3 deletions

12
common/stats.go Normal file
View File

@@ -0,0 +1,12 @@
package common
var OnlineUsers int
func init(){
OnlineUsers = 0
}
func OnlineStatUpdate(t int) {
OnlineUsers += t
}

View File

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

View File

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