From c3bb2472e5864a4c2d27422718c7c678fe3e342e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=8C=98WinKey?= <44907675+Ynng@users.noreply.github.com> Date: Fri, 16 Aug 2024 02:13:03 -0400 Subject: [PATCH] Add an endpoint to clear an instance's console buffer Fixes #299 Add an endpoint to clear an instance's console buffer by clearing the circular buffer within the global app state buffer. * **core/src/lib.rs** - Add a method `clear_console_buffer` to the `AppState` struct to clear the console buffer for a specific instance. * **core/src/handlers/events.rs** - Add a new function `clear_console_buffer` to handle the request to clear the console buffer. - Add a new route `/instance/:uuid/console/clear` to handle the request to clear the console buffer. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Lodestone-Team/lodestone/issues/299?shareId=XXXX-XXXX-XXXX-XXXX). --- core/src/handlers/events.rs | 25 +++++++++++++++++++++++++ core/src/lib.rs | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/core/src/handlers/events.rs b/core/src/handlers/events.rs index 7400d741..fcdfc065 100644 --- a/core/src/handlers/events.rs +++ b/core/src/handlers/events.rs @@ -287,6 +287,30 @@ async fn console_stream_ws( } } +pub async fn clear_console_buffer( + axum::extract::State(state): axum::extract::State, + AuthBearer(token): AuthBearer, + Path(uuid): Path, +) -> Result, Error> { + let requester = state + .users_manager + .read() + .await + .try_auth(&token) + .ok_or_else(|| Error { + kind: ErrorKind::Unauthorized, + source: eyre!("Token error"), + })?; + if !requester.can_perform_action(&UserAction::ClearConsoleBuffer(uuid.clone())) { + return Err(Error { + kind: ErrorKind::PermissionDenied, + source: eyre!("You don't have permission to clear the console buffer"), + }); + } + state.clear_console_buffer(&uuid).await; + Ok(Json(())) +} + pub fn get_events_routes(state: AppState) -> Router { Router::new() .route("/events/:uuid/stream", get(event_stream)) @@ -294,5 +318,6 @@ pub fn get_events_routes(state: AppState) -> Router { .route("/events/search", get(get_event_search)) .route("/instance/:uuid/console/stream", get(console_stream)) .route("/instance/:uuid/console/buffer", get(get_console_buffer)) + .route("/instance/:uuid/console/clear", get(clear_console_buffer)) .with_state(state) } diff --git a/core/src/lib.rs b/core/src/lib.rs index 6f11964f..506c23db 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -133,6 +133,10 @@ impl AppState { }); } } + + pub async fn clear_console_buffer(&self, uuid: &InstanceUuid) { + self.console_out_buffer.lock().await.remove(uuid); + } } async fn restore_instances(