add user count backend

This commit is contained in:
Radon 2025-09-04 09:10:05 -05:00
parent de0d206e2f
commit 7fe2bfa95a
2 changed files with 20 additions and 0 deletions

16
hub.go
View File

@ -25,6 +25,22 @@ func IsUsernameTaken(h *server.Hub, username string) bool {
return false
}
func HandleUserCountCheck(hub *server.Hub, w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
response := map[string]any{
"userCount": int64(len(hub.Clients)),
}
err := json.NewEncoder(w).Encode(response)
if err != nil {
return
}
}
func HandleUsernameCheck(hub *server.Hub, w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)

View File

@ -50,6 +50,10 @@ func main() {
HandleFileDownload(filesDir, w, r)
})
mux.HandleFunc("/user-count", func(w http.ResponseWriter, r *http.Request) {
HandleUserCountCheck(hub, w, r)
})
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
HandleWebSocket(hub, w, r)
})