Skip to content

Commit

Permalink
Add an endpoint to clear an instance's console buffer
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Ynng committed Aug 16, 2024
1 parent 4b88f48 commit c3bb247
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions core/src/handlers/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,37 @@ async fn console_stream_ws(
}
}

pub async fn clear_console_buffer(
axum::extract::State(state): axum::extract::State<AppState>,
AuthBearer(token): AuthBearer,
Path(uuid): Path<InstanceUuid>,
) -> Result<Json<()>, 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())) {

Check failure on line 304 in core/src/handlers/events.rs

View workflow job for this annotation

GitHub Actions / clippy

failed to resolve: use of undeclared type `UserAction`

error[E0433]: failed to resolve: use of undeclared type `UserAction` --> core/src/handlers/events.rs:304:39 | 304 | if !requester.can_perform_action(&UserAction::ClearConsoleBuffer(uuid.clone())) { | ^^^^^^^^^^ use of undeclared type `UserAction` | help: consider importing this enum | 1 + use crate::auth::user::UserAction; |
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))
.route("/events/:uuid/buffer", get(get_event_buffer))
.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)
}
4 changes: 4 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c3bb247

Please sign in to comment.