add user count backend
This commit is contained in:
parent
de0d206e2f
commit
7fe2bfa95a
16
hub.go
16
hub.go
@ -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)
|
||||
|
4
main.go
4
main.go
@ -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)
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user