hive-agent: replace json! with typed structs in web_ui handlers
This commit is contained in:
parent
127846ef1b
commit
786e4610f0
3 changed files with 34 additions and 15 deletions
|
|
@ -5,11 +5,23 @@ use std::convert::Infallible;
|
|||
use axum::Json;
|
||||
use axum::extract::{Query, State};
|
||||
use axum::response::sse::{Event, KeepAlive, Sse};
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio_stream::{Stream, StreamExt, wrappers::BroadcastStream};
|
||||
|
||||
use super::AppState;
|
||||
|
||||
/// Response body for `GET /api/events/history`. `seq` is omitted from the
|
||||
/// wire entirely on a paginated (non-initial) load — matches the old
|
||||
/// `json!` shape, which only ever set the `"seq"` key when `Some`.
|
||||
#[derive(Serialize)]
|
||||
pub(super) struct EventsHistoryBody {
|
||||
events: Vec<crate::events::StoredEvent>,
|
||||
min_id: Option<i64>,
|
||||
has_more: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
seq: Option<u64>,
|
||||
}
|
||||
|
||||
/// Query params for the paginated history endpoint.
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub(super) struct HistoryParams {
|
||||
|
|
@ -23,7 +35,7 @@ pub(super) struct HistoryParams {
|
|||
pub(super) async fn events_history(
|
||||
State(state): State<AppState>,
|
||||
Query(params): Query<HistoryParams>,
|
||||
) -> Json<serde_json::Value> {
|
||||
) -> Json<EventsHistoryBody> {
|
||||
use crate::events::HISTORY_CAPACITY;
|
||||
let limit = params.limit.unwrap_or(100).min(HISTORY_CAPACITY);
|
||||
let before = params.before;
|
||||
|
|
@ -51,15 +63,12 @@ pub(super) async fn events_history(
|
|||
se
|
||||
})
|
||||
.collect();
|
||||
let mut resp = serde_json::json!({
|
||||
"events": events,
|
||||
"min_id": min_id,
|
||||
"has_more": has_more,
|
||||
});
|
||||
if let Some(s) = seq {
|
||||
resp["seq"] = serde_json::json!(s);
|
||||
}
|
||||
Json(resp)
|
||||
Json(EventsHistoryBody {
|
||||
events,
|
||||
min_id,
|
||||
has_more,
|
||||
seq,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) async fn events_stream(
|
||||
|
|
|
|||
Loading…
Reference in a new issue