From a3b55f00df30eb0fb120dd09acaedf0d91842d43 Mon Sep 17 00:00:00 2001 From: Romain Mallard Date: Fri, 26 Sep 2025 04:05:22 +0200 Subject: [PATCH] websocket for single db update --- db/1.sqlite | Bin 40960 -> 40960 bytes db/auth.sqlite | Bin 28672 -> 28672 bytes src/main.rs | 10 +++------- src/rooms/handler.rs | 24 ++++++++++++++++++++++-- src/utils/db_pool.rs | 21 ++++++++++----------- src/utils/websocket.rs | 6 +++--- 6 files changed, 38 insertions(+), 23 deletions(-) diff --git a/db/1.sqlite b/db/1.sqlite index 189b6e1de7618a35a5ef5049cb431a468e71de45..9dcef9e8b61225675d98cf87e8d47cdf1b07f663 100644 GIT binary patch delta 62 zcmZoTz|?SnX@WGP>_i!7M%j%C%lJ8VF!1l-f5Lxtv!K8#esO6=76xHcLj%L49RJcx RAjr+m&B@N%d|uwL0RUtP6f6J$ delta 53 zcmZoTz|?SnX@WGP^h6nFM(K?S%lJ7CGVmYdf5Lxtv!KFeem)^a76xHuLj%L4oW!J@ J&FAF}8vwy55O4qh diff --git a/db/auth.sqlite b/db/auth.sqlite index cd1d46dcafad9df484dd03e03d1130c077e2a2a6..555f62a8b95857bbbfd077da91df95c1bdca9270 100644 GIT binary patch delta 349 zcmZp8z}WDBae_1>>qHr6R#pbRs=|#a3;4O1`577bpYa#-Gj0|X_{_&!z{JiV+UPIJ zS(Kk^Jh@R`-Yp=vtk^rT#HFghEY&N((>KGdq{t&(r68axvCP0#Kd>aP$fzhSFDfr2 zF*h_K!YIo*tGqI;*f`HO(y1)cD8tkdY*exQM4)A#82JD2|KR_$Sy15#zYHrgBO?0QrX=j@J)?OT_UyFfXi+>{jcc5cs z_*v6f8vPkTj&!EnkxY{(`WFLj0NVc*&-o_?uyAuSGK({o7N-^! GGXVf0ENM{y delta 154 zcmZp8z}WDBae_1>%S0JxRu%@mHqDJG3-~#h_=_3%pYa!O7F0;(XDVi#Tr58kDENbc z{}2C<&4LL}_$LN%h%hsAaxyb>aBwj&Ffj4IW#E6y{|%_)K0gx|^W?Yo;y@h}8Tcpi ve+LS-@=u=VUknrhD*MX+7AW$NfAZUSNtSO6AWaYxxi}b^#W_ohQ;UiLNjNQ_ diff --git a/src/main.rs b/src/main.rs index 3344437..b0828a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,9 +21,12 @@ use crate::utils::auth::JwtKeys; + #[tokio::main] async fn main() -> std::io::Result<()> { + + let hotel_pools = HotelPool::new(); let logs_manager = SqliteConnectionManager::file("db/auth.sqlite"); let logs_pool = Pool::builder() @@ -31,13 +34,6 @@ async fn main() -> std::io::Result<()> { .build(logs_manager) .expect("Failed to build logs pool"); - type UserMap = DashMap>; - /// hotel_id → users - type HotelMap = DashMap>; - /// global map of all hotels - type WsMap = Arc; - - let state = AppState { hotel_pools, logs_pool, diff --git a/src/rooms/handler.rs b/src/rooms/handler.rs index 465e43e..5429e57 100644 --- a/src/rooms/handler.rs +++ b/src/rooms/handler.rs @@ -1,6 +1,8 @@ use axum::{Json, extract::Path, extract::State }; use axum::response::IntoResponse; use axum::http::StatusCode; +use axum::extract::ws::Message; +use serde_json::json; use crate::rooms::extractor::UpdateRoomPayload; //use crate::utils::db_pool::*; @@ -85,9 +87,27 @@ pub async fn clean_db_update( ); match result { - Ok(rows) if rows > 0 => (StatusCode::OK, format!("updated room {room_id} in hotel{}, with status : {}", hotel_id, payload.status )), + Ok(rows) if rows > 0 => { + // --- broadcast to all WS clients in the hotel --- + if let Some(hotel_users) = state.ws_map.get(&hotel_id) { + let update_msg = json!({ + "room_id": room_id, + "status": payload.status, + "updated_by": user_id, + }) + .to_string(); + + for entry in hotel_users.iter() { + let sender = entry.value(); + // ignore errors (client disconnected) + let _ = sender.send(Message::Text(update_msg.clone().into())); + } + } + + (StatusCode::OK, format!("updated room {room_id} in hotel {hotel_id}, with status: {}", payload.status)) + } Ok(_) => (StatusCode::NOT_FOUND, "No room found".to_string()), - Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB : {err}")), + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB: {err}")), } } diff --git a/src/utils/db_pool.rs b/src/utils/db_pool.rs index aa51e04..6be8c1f 100644 --- a/src/utils/db_pool.rs +++ b/src/utils/db_pool.rs @@ -5,19 +5,18 @@ use r2d2_sqlite::SqliteConnectionManager; use tokio::sync::mpsc; use axum::extract::{ws::{Message, WebSocket, WebSocketUpgrade}, State}; +use crate::utils::websocket::WsMap; + + #[derive(Clone)] + pub struct AppState { + pub hotel_pools: HotelPool, + pub logs_pool: Pool, + pub ws_map: WsMap, + } + + type HotelId = i32; // or i32 if you want numeric ids - - -#[derive(Clone)] -pub struct AppState { - pub hotel_pools: HotelPool, - pub logs_pool: Pool, - pub ws_map: WsMap, - -} - - #[derive(Clone)] pub struct HotelPool { hotel_pools: Arc>>, diff --git a/src/utils/websocket.rs b/src/utils/websocket.rs index 64867f4..793cdb5 100644 --- a/src/utils/websocket.rs +++ b/src/utils/websocket.rs @@ -12,11 +12,11 @@ use crate::utils::{auth::AuthClaims, db_pool::{AppState, HotelPool}}; /// Type alias: user_id → sender to that user -type UserMap = DashMap>; +pub type UserMap = DashMap>; /// hotel_id → users -type HotelMap = DashMap>; +pub type HotelMap = DashMap>; /// global map of all hotels -type WsMap = Arc; +pub type WsMap = Arc; /// Type alias: user_id → sender to that user