update logging system and send websocket message on owner creation via api

This commit is contained in:
Ninjdai 2025-08-01 11:14:04 +02:00
parent 3e1c744db1
commit c016fc915b
7 changed files with 97 additions and 53 deletions

View file

@ -73,7 +73,7 @@ pub async fn create_book_instance(
let b = book_instance.save(state.db_conn.as_ref()).await;
match b {
Err(e) => {
println!("Error while inserting new book instance: {:#?}", e);
log::error!(target: "api", "Error while inserting new book instance: {:#?}", e);
(StatusCode::BAD_REQUEST, Json(None))
},
Ok(res) => (StatusCode::OK, Json(Some(res.try_into_model().expect("All fields should be set once the book instance is saved"))))

View file

@ -6,7 +6,7 @@ use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, EntityTrait, TryInto
use serde::{Deserialize, Serialize};
use utoipa::IntoParams;
use crate::{entities::{owner, prelude::Owner}, AppState};
use crate::{entities::{owner, prelude::Owner}, utils::events::{Event, WebsocketMessage}, AppState};
#[derive(IntoParams)]
@ -62,19 +62,23 @@ pub async fn create_owner(
Json(instance_payload): Json<OwnerCreateParams>,
) -> (StatusCode, Json<Option<owner::Model>>) {
let book_instance = owner::ActiveModel {
let owner = owner::ActiveModel {
first_name: Set(instance_payload.first_name),
last_name: Set(instance_payload.last_name),
contact: Set(instance_payload.contact),
id: NotSet
};
let b = book_instance.save(state.db_conn.as_ref()).await;
match b {
let o = owner.insert(state.db_conn.as_ref()).await;
match o {
Err(e) => {
println!("Error while inserting new owner: {:#?}", e);
log::error!(target: "api", "Error while inserting new owner from api: {:#?}", e);
(StatusCode::BAD_REQUEST, Json(None))
},
Ok(res) => (StatusCode::OK, Json(Some(res.try_into_model().expect("All fields should be set once the book instance is saved"))))
Ok(res) => {
let model = res.try_into_model().expect("All fields should be set once the owner is saved");
let _ = state.event_bus.send(Event::WebsocketBroadcast(WebsocketMessage::NewOwner(Arc::new(model.clone()))));
(StatusCode::OK, Json(Some(model)))
}
}
}

View file

@ -9,7 +9,6 @@ use axum::{
},
response::IntoResponse
};
use serde_json::json;
use crate::{utils::events, AppState};
@ -21,9 +20,7 @@ pub async fn ws_handler(
ConnectInfo(addr): ConnectInfo<SocketAddr>,
State(state): State<Arc<AppState>>
) -> impl IntoResponse {
println!("`{addr} connected.");
// finalize the upgrade process by returning upgrade callback.
// we can customize the callback by sending additional info such as address.
log::debug!(target: "websocket", "{addr} connected.");
ws.on_upgrade(move |socket| handle_socket(socket, addr, state))
}
@ -34,9 +31,9 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr, state: Arc<AppSta
.await
.is_ok()
{
println!("WS >>> Pinged {who}...");
log::debug!(target: "websocket", "Pinged {who}...");
} else {
println!("WS >>> Could not send ping {who}!");
log::debug!(target: "websocket", "Could not send ping to {who}!");
return;
}
@ -46,7 +43,7 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr, state: Arc<AppSta
return;
}
} else {
println!("WS >>> Client {who} abruptly disconnected");
log::debug!(target: "websocket", "Client {who} abruptly disconnected");
return;
}
}
@ -78,15 +75,15 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr, state: Arc<AppSta
tokio::select! {
rv_a = (&mut send_task) => {
match rv_a {
Ok(()) => println!("WS >>> Sender connection with {who} gracefully shut down"),
Err(a) => println!("WS >>> Error sending messages {a:?}")
Ok(()) => log::debug!(target: "websocket", "Sender connection with {who} gracefully shut down"),
Err(a) => log::debug!(target: "websocket", "Error sending messages {a:?}")
}
recv_task.abort();
},
rv_b = (&mut recv_task) => {
match rv_b {
Ok(()) => println!("WS >>> Receiver connection with {who} gracefully shut down"),
Err(b) => println!("WS >>> Error receiving messages {b:?}")
Ok(()) => log::debug!(target: "websocket", "Receiver connection with {who} gracefully shut down"),
Err(b) => log::debug!(target: "websocket", "Error receiving messages {b:?}")
}
send_task.abort();
}
@ -96,28 +93,27 @@ async fn handle_socket(mut socket: WebSocket, who: SocketAddr, state: Arc<AppSta
fn process_message(msg: Message, who: SocketAddr) -> ControlFlow<(), ()> {
match msg {
Message::Text(t) => {
println!("WS >>> {who} sent str: {t:?}");
log::debug!(target: "websocket", "{who} sent str: {t:?}");
}
Message::Binary(d) => {
println!("WS >>> {who} sent {} bytes: {d:?}", d.len());
log::debug!(target: "websocket", "{who} sent {} bytes: {d:?}", d.len());
}
Message::Close(c) => {
if let Some(cf) = c {
println!(
"WS >>> {who} sent close with code {} and reason `{}`",
log::debug!(target: "websocket",
"{who} sent close with code {} and reason `{}`",
cf.code, cf.reason
);
} else {
println!("WS >>> {who} somehow sent close message without CloseFrame");
log::debug!(target: "websocket", "{who} somehow sent close message without CloseFrame");
}
return ControlFlow::Break(());
}
Message::Pong(v) => {
println!("WS >>> {who} sent pong with {v:?}");
Message::Pong(_v) => {
//log::debug!(target: "websocket", "{who} sent pong with {v:?}");
}
Message::Ping(v) => {
println!("WS >>> {who} sent ping with {v:?}");
Message::Ping(_v) => {
//log::debug!(target: "websocket", "{who} sent ping with {v:?}");
}
}
ControlFlow::Continue(())