m/cup
1
0
mirror of https://github.com/sergi0g/cup.git synced 2025-11-17 17:43:37 -05:00

chore: remove v2 API endpoints

This commit is contained in:
Sergio
2025-05-28 19:55:21 +03:00
parent b542f1bac5
commit eaf2cd7881
4 changed files with 14 additions and 48 deletions

View File

@@ -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));
}

View File

@@ -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<Mutex<ServerData>>>, path: PathRef<'_>)
}
}
async fn api_simple(data: StateRef<'_, Arc<Mutex<ServerData>>>) -> WebResponse {
async fn json(data: StateRef<'_, Arc<Mutex<ServerData>>>) -> 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<Mutex<ServerData>>>) -> 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<Mutex<ServerData>>>) -> WebResponse {
struct ServerData {
template: String,
raw_updates: Vec<Update>,
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()

View File

@@ -104,4 +104,4 @@ impl Update {
status => status.clone(),
}
}
}
}

View File

@@ -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::<Vec<Value>>(),