Skip to content

Commit c3bb247

Browse files
committed
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).
1 parent 4b88f48 commit c3bb247

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

core/src/handlers/events.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,37 @@ async fn console_stream_ws(
287287
}
288288
}
289289

290+
pub async fn clear_console_buffer(
291+
axum::extract::State(state): axum::extract::State<AppState>,
292+
AuthBearer(token): AuthBearer,
293+
Path(uuid): Path<InstanceUuid>,
294+
) -> Result<Json<()>, Error> {
295+
let requester = state
296+
.users_manager
297+
.read()
298+
.await
299+
.try_auth(&token)
300+
.ok_or_else(|| Error {
301+
kind: ErrorKind::Unauthorized,
302+
source: eyre!("Token error"),
303+
})?;
304+
if !requester.can_perform_action(&UserAction::ClearConsoleBuffer(uuid.clone())) {
305+
return Err(Error {
306+
kind: ErrorKind::PermissionDenied,
307+
source: eyre!("You don't have permission to clear the console buffer"),
308+
});
309+
}
310+
state.clear_console_buffer(&uuid).await;
311+
Ok(Json(()))
312+
}
313+
290314
pub fn get_events_routes(state: AppState) -> Router {
291315
Router::new()
292316
.route("/events/:uuid/stream", get(event_stream))
293317
.route("/events/:uuid/buffer", get(get_event_buffer))
294318
.route("/events/search", get(get_event_search))
295319
.route("/instance/:uuid/console/stream", get(console_stream))
296320
.route("/instance/:uuid/console/buffer", get(get_console_buffer))
321+
.route("/instance/:uuid/console/clear", get(clear_console_buffer))
297322
.with_state(state)
298323
}

core/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ impl AppState {
133133
});
134134
}
135135
}
136+
137+
pub async fn clear_console_buffer(&self, uuid: &InstanceUuid) {
138+
self.console_out_buffer.lock().await.remove(uuid);
139+
}
136140
}
137141

138142
async fn restore_instances(

0 commit comments

Comments
 (0)