Skip to content

Commit

Permalink
server: Add websocket for log information
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Nov 10, 2024
1 parent 6d57cb3 commit d0fdc9b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/server/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub async fn run(server_address: &str) -> Result<(), std::io::Error> {
)
.route("/xml", web::get().to(pages::xml))
.route("/sdp", web::get().to(pages::sdp))
.route("/log", web::get().to(pages::log))
.service(
web::scope("/thumbnail")
// Add a rate limitter to prevent flood
Expand Down
26 changes: 25 additions & 1 deletion src/lib/server/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use std::io::prelude::*;

use actix_web::{
http::header,
rt,
web::{self, Json},
HttpRequest, HttpResponse,
Error, HttpRequest, HttpResponse,
};
use paperclip::actix::{api_v2_operation, Apiv2Schema, CreatedJson};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -465,3 +466,26 @@ pub async fn gst_info() -> HttpResponse {
.body(format!("{error:#?}")),
}
}

#[api_v2_operation]
/// Provides a access point for the service log
pub async fn log(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
let (response, mut session, _stream) = actix_ws::handle(&req, stream)?;
rt::spawn(async move {
let (mut receiver, history) = crate::logger::manager::HISTORY.lock().unwrap().subscribe();

for message in history {
if session.text(message).await.is_err() {
return;
}
}

while let Ok(message) = receiver.recv().await {
if session.text(message).await.is_err() {
return;
}
}
});

Ok(response)
}

0 comments on commit d0fdc9b

Please sign in to comment.