From 63bc49ccf67a250fc61154597ef6956110427617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 11 May 2025 20:01:43 +0300 Subject: [PATCH] feat: :sparkles: online counter --- server/src/signal.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server/src/signal.rs b/server/src/signal.rs index 299c99b..a545805 100644 --- a/server/src/signal.rs +++ b/server/src/signal.rs @@ -4,7 +4,7 @@ use std::{ time::Duration, }; -use axum::{Router, http::StatusCode, response::IntoResponse, routing::get}; +use axum::{Json, Router, http::StatusCode, response::IntoResponse, routing::get}; use fastwebsockets::{ Frame, OpCode, WebSocketError, upgrade::{IncomingUpgrade, UpgradeFut}, @@ -32,6 +32,7 @@ pub async fn start_signalling() { let router = Router::new() .route("/", get(alive)) .route("/signal", get(signal)) + .route("/count/online", get(online_counter)) .layer(CorsLayer::permissive()); let listener = TcpListener::bind(SERVER_ADDRESS).await.unwrap(); @@ -44,6 +45,11 @@ async fn alive() -> impl IntoResponse { StatusCode::OK } +async fn online_counter() -> impl IntoResponse { + let online_count = ONLINE_USERS.read().await.len(); + (StatusCode::OK, Json(online_count)) +} + async fn signal(websocket: IncomingUpgrade) -> impl IntoResponse { let (response, websocket) = websocket.upgrade().unwrap(); tokio::spawn(websocket_handler(websocket));