diff --git a/src/lib/server/manager.rs b/src/lib/server/manager.rs index 6de51d30..72b2f890 100644 --- a/src/lib/server/manager.rs +++ b/src/lib/server/manager.rs @@ -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 diff --git a/src/lib/server/pages.rs b/src/lib/server/pages.rs index 0b853a21..27d5b89d 100644 --- a/src/lib/server/pages.rs +++ b/src/lib/server/pages.rs @@ -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}; @@ -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 { + 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) +}