Compare commits
2 Commits
22d4596162
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b7d25ef259 | |||
| 9ee400041f |
72
src/api.rs
72
src/api.rs
@@ -1,72 +0,0 @@
|
|||||||
use serde_json::Value;
|
|
||||||
use reqwest::{self, Error};
|
|
||||||
|
|
||||||
pub const BASE_URL: &str = "https://uptime.shockrah.xyz";
|
|
||||||
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! heartbeat {
|
|
||||||
($slug:expr) => { format!("{}/api/status-page/heartbeat/{}", crate::api::BASE_URL, $slug) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! endpoints {
|
|
||||||
($slug:expr) => { format!("{}/api/status-page/{}", crate::api::BASE_URL, $slug) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct HeartBeat {
|
|
||||||
status: i64,
|
|
||||||
time: String,
|
|
||||||
msg: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct KumaMonitor {
|
|
||||||
id: i64,
|
|
||||||
name: String,
|
|
||||||
heartbeats: Option<Vec<HeartBeat>>
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct KumaStatusPage {
|
|
||||||
slug: String,
|
|
||||||
title: String,
|
|
||||||
description: String,
|
|
||||||
monitors: Vec<KumaMonitor>
|
|
||||||
}
|
|
||||||
|
|
||||||
impl KumaMonitor {
|
|
||||||
pub fn blank(val: &Value) -> Self {
|
|
||||||
Self {
|
|
||||||
id: val["id"].as_i64().unwrap_or(0),
|
|
||||||
name: val["name"].to_string(),
|
|
||||||
heartbeats: None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl KumaStatusPage {
|
|
||||||
fn get_monitors(json: &Value) -> Vec<KumaMonitor> {
|
|
||||||
let mut monitors = vec![];
|
|
||||||
for group in json["publicGroupList"].as_array().unwrap_or(&vec![]) {
|
|
||||||
for monitor in group["monitorList"].as_array().unwrap_or(&vec![]) {
|
|
||||||
monitors.push(KumaMonitor::blank(&monitor));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return monitors;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get(slug: &str) -> Result<Self, Error> {
|
|
||||||
let endpoint = endpoints!(slug);
|
|
||||||
let resp: Value = reqwest::get(&endpoint).await?.json().await?;
|
|
||||||
return Ok(KumaStatusPage {
|
|
||||||
slug: resp["config"]["slug"].to_string(),
|
|
||||||
title: resp["config"]["title"].to_string(),
|
|
||||||
description: resp["config"]["description"].to_string(),
|
|
||||||
monitors: KumaStatusPage::get_monitors(&resp)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
104
src/kuma/api.rs
Normal file
104
src/kuma/api.rs
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
use serde_json::Value;
|
||||||
|
use serde::Serialize;
|
||||||
|
use reqwest::{self, Error};
|
||||||
|
|
||||||
|
/// Target base URL to use for all KUMA API call's
|
||||||
|
pub const BASE_URL: &str = "https://uptime.shockrah.xyz";
|
||||||
|
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! heartbeat {
|
||||||
|
($slug:expr) => { format!("{}/api/status-page/heartbeat/{}", BASE_URL, $slug) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! endpoints {
|
||||||
|
($slug:expr) => { format!("{}/api/status-page/{}", BASE_URL, $slug) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A single heartbeat within a monitor's latest status
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct HeartBeat {
|
||||||
|
status: i64,
|
||||||
|
time: String,
|
||||||
|
msg: String,
|
||||||
|
ping: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A monitor which contains the recent heartbeats for that monitor
|
||||||
|
/// Heartbeat list themselves will always be populated and can be configured
|
||||||
|
/// to have a maximum size with the MAX_HEARTBEATS env variable
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct KumaMonitor {
|
||||||
|
id: i64,
|
||||||
|
name: String,
|
||||||
|
// TODO: make sure we support MAX_HEARTBEATS somehow
|
||||||
|
heartbeats: Vec<HeartBeat>
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The overall look of the Kuma status page
|
||||||
|
/// For now we only have pub however we can control which we are loking at
|
||||||
|
/// with the use of the `slug` field.
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct KumaStatusPage {
|
||||||
|
slug: String,
|
||||||
|
title: String,
|
||||||
|
description: String,
|
||||||
|
monitors: Vec<KumaMonitor>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KumaMonitor {
|
||||||
|
/// Generates a full monitor object with the most recent available heartbeats
|
||||||
|
pub async fn new(val: &Value) -> Result<Self, Error> {
|
||||||
|
// Populate the monitor with it's respective heartbeats at that time
|
||||||
|
let id = val["id"].as_i64().unwrap_or(0);
|
||||||
|
let name = val["name"].as_str().unwrap().into();
|
||||||
|
let response: Value = reqwest::get(heartbeat!("pub")).await?.json().await?;
|
||||||
|
if let Some(list) = &response["heartbeatList"][id.to_string()].as_array() {
|
||||||
|
let heartbeats = list.iter().map(|item| {
|
||||||
|
HeartBeat {
|
||||||
|
status: item["status"].as_i64().unwrap_or(-1),
|
||||||
|
time: item["time"].to_string().replace("\"", ""),
|
||||||
|
msg: item["msg"].to_string().replace("\"", ""),
|
||||||
|
ping: item["ping"].as_i64().unwrap_or(-1),
|
||||||
|
}
|
||||||
|
}).collect();
|
||||||
|
return Ok(Self { id, name, heartbeats })
|
||||||
|
}
|
||||||
|
// TODO: wtf should a default response be?
|
||||||
|
// temporary blank response for now
|
||||||
|
Ok(Self { id, name, heartbeats: vec![] })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl KumaStatusPage {
|
||||||
|
/// Monitors require their own logic to fetch with their heartbeats on load.
|
||||||
|
/// This func is only really called by Self::get when we are first loading
|
||||||
|
/// a new status page for the first time
|
||||||
|
async fn get_monitors(json: &Value) -> Vec<KumaMonitor> {
|
||||||
|
let mut monitors = vec![];
|
||||||
|
for group in json["publicGroupList"].as_array().unwrap_or(&vec![]) {
|
||||||
|
for monitor in group["monitorList"].as_array().unwrap_or(&vec![]) {
|
||||||
|
if let Ok(mon) = KumaMonitor::new(&monitor).await {
|
||||||
|
monitors.push(mon);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return monitors;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Main entrypoint for KumaStatePage API hits. Here we get the basics of the page
|
||||||
|
/// along with any and all heartbeats for the monitors on that page
|
||||||
|
pub async fn get(slug: &str) -> Result<Self, Error> {
|
||||||
|
let endpoint = endpoints!(slug);
|
||||||
|
let resp: Value = reqwest::get(&endpoint).await?.json().await?;
|
||||||
|
return Ok(KumaStatusPage {
|
||||||
|
slug: resp["config"]["slug"].as_str().unwrap().into(),
|
||||||
|
title: resp["config"]["title"].as_str().unwrap().into(),
|
||||||
|
description: resp["config"]["description"].as_str().unwrap().into(),
|
||||||
|
monitors: KumaStatusPage::get_monitors(&resp).await
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
2
src/kuma/mod.rs
Normal file
2
src/kuma/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod api;
|
||||||
|
pub mod data;
|
||||||
14
src/main.rs
14
src/main.rs
@@ -1,14 +1,18 @@
|
|||||||
mod data;
|
mod kuma;
|
||||||
mod api;
|
|
||||||
|
|
||||||
use reqwest::{self, Error};
|
use reqwest::{self, Error};
|
||||||
use crate::api::KumaStatusPage;
|
use crate::kuma::api::KumaStatusPage;
|
||||||
|
use serde_json;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Error> {
|
async fn main() -> Result<(), Error> {
|
||||||
let page = KumaStatusPage::get("pub").await?;
|
let public = KumaStatusPage::get("pub").await?;
|
||||||
println!("{page:?}");
|
// Debugging the initial API response
|
||||||
|
match serde_json::to_string(&public) {
|
||||||
|
Ok(result) => println!("{result}"),
|
||||||
|
Err(_) => eprintln!("bruh")
|
||||||
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user