diff --git a/src/formatting/mod.rs b/src/formatting/mod.rs index ad1c695..de8c438 100644 --- a/src/formatting/mod.rs +++ b/src/formatting/mod.rs @@ -7,7 +7,7 @@ use crate::{ status::Status, update::{Update, UpdateInfo}, }, - utils::{json::to_simple_json, sort_update_vec::sort_update_vec}, + utils::{json::to_json, sort_update_vec::sort_update_vec}, }; pub fn print_updates(updates: &[Update], icons: &bool) { @@ -164,5 +164,5 @@ pub fn print_updates(updates: &[Update], icons: &bool) { } pub fn print_raw_updates(updates: &[Update]) { - println!("{}", to_simple_json(updates)); + println!("{}", to_json(updates)); } diff --git a/src/server.rs b/src/server.rs index 03d2063..da50feb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -24,7 +24,7 @@ use crate::{ error, structs::update::Update, utils::{ - json::{to_full_json, to_simple_json}, + json::to_json, sort_update_vec::sort_update_vec, time::{elapsed, now}, }, @@ -91,9 +91,7 @@ pub async fn serve(port: &u16, ctx: &Context) -> std::io::Result<()> { ctx.logger.info("Ready to start!"); let mut app_builder = App::new() .with_state(data) - .at("/api/v2/json", get(handler_service(api_simple))) - .at("/api/v3/json", get(handler_service(api_full))) - .at("/api/v2/refresh", get(handler_service(refresh))) + .at("/api/v3/json", get(handler_service(json))) .at("/api/v3/refresh", get(handler_service(refresh))); if !ctx.config.agent { app_builder = app_builder @@ -148,20 +146,11 @@ async fn _static(data: StateRef<'_, Arc>>, path: PathRef<'_>) } } -async fn api_simple(data: StateRef<'_, Arc>>) -> WebResponse { +async fn json(data: StateRef<'_, Arc>>) -> WebResponse { WebResponse::builder() .header("Content-Type", "application/json") .body(ResponseBody::from( - data.lock().await.simple_json.clone().to_string(), - )) - .unwrap() -} - -async fn api_full(data: StateRef<'_, Arc>>) -> WebResponse { - WebResponse::builder() - .header("Content-Type", "application/json") - .body(ResponseBody::from( - data.lock().await.full_json.clone().to_string(), + data.lock().await.json.clone().to_string(), )) .unwrap() } @@ -174,8 +163,7 @@ async fn refresh(data: StateRef<'_, Arc>>) -> WebResponse { struct ServerData { template: String, raw_updates: Vec, - simple_json: Value, - full_json: Value, + json: Value, ctx: Context, theme: &'static str, } @@ -185,8 +173,7 @@ impl ServerData { let mut s = Self { ctx: ctx.clone(), template: String::new(), - simple_json: Value::Null, - full_json: Value::Null, + json: Value::Null, raw_updates: Vec::new(), theme: "neutral", }; @@ -210,19 +197,17 @@ impl ServerData { .unwrap() .parse(HTML) .unwrap(); - self.simple_json = to_simple_json(&self.raw_updates); - self.full_json = to_full_json(&self.raw_updates); + self.json = to_json(&self.raw_updates); let last_updated = Local::now(); - self.simple_json["last_updated"] = last_updated + self.json["last_updated"] = last_updated .to_rfc3339_opts(chrono::SecondsFormat::Secs, true) .to_string() .into(); - self.full_json["last_updated"] = self.simple_json["last_updated"].clone(); self.theme = match &self.ctx.config.theme { Theme::Default => "neutral", Theme::Blue => "gray", }; - let mut metrics = self.simple_json["metrics"] + let mut metrics = self.json["metrics"] .as_object() .unwrap() .iter() diff --git a/src/structs/update.rs b/src/structs/update.rs index 89f3c48..d16b98c 100644 --- a/src/structs/update.rs +++ b/src/structs/update.rs @@ -104,4 +104,4 @@ impl Update { status => status.clone(), } } -} +} \ No newline at end of file diff --git a/src/utils/json.rs b/src/utils/json.rs index 3a38cfe..1538364 100644 --- a/src/utils/json.rs +++ b/src/utils/json.rs @@ -1,6 +1,6 @@ // Functions that return JSON data, used for generating output and API responses -use serde_json::{json, Map, Value}; +use serde_json::{json, Value}; use crate::structs::{status::Status, update::Update}; @@ -47,27 +47,8 @@ pub fn get_metrics(updates: &[Update]) -> Value { }) } -/// Takes a slice of `Image` objects and returns a `Value` with update info. The output doesn't contain much detail -pub fn to_simple_json(updates: &[Update]) -> Value { - let mut update_map = Map::new(); - updates.iter().for_each(|update| { - let _ = update_map.insert( - update.reference.clone(), - match update.result.has_update { - Some(has_update) => Value::Bool(has_update), - None => Value::Null, - }, - ); - }); - let json_data: Value = json!({ - "metrics": get_metrics(updates), - "images": updates, - }); - json_data -} - /// Takes a slice of `Image` objects and returns a `Value` with update info. All image data is included, useful for debugging. -pub fn to_full_json(updates: &[Update]) -> Value { +pub fn to_json(updates: &[Update]) -> Value { json!({ "metrics": get_metrics(updates), "images": updates.iter().map(|update| serde_json::to_value(update).unwrap()).collect::>(),