feat: online counter

This commit is contained in:
Ahmet Kaan Gümüş 2025-05-11 20:01:43 +03:00
parent ab51292439
commit 63bc49ccf6

View file

@ -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));