Reorganizing files
This commit is contained in:
parent
1c4077ffc3
commit
883a0669fe
11 changed files with 105 additions and 45 deletions
40
src/api/client.rs
Normal file
40
src/api/client.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use anyhow::Result;
|
||||
use reqwest::blocking::Client;
|
||||
|
||||
use crate::{
|
||||
api::endpoints::UptimeKumaEndpoints,
|
||||
data::{self, heartbeat::HeartbeatResponse, status_page::model::StatusPageResponse},
|
||||
};
|
||||
|
||||
pub struct UptimeKumaClient {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl UptimeKumaClient {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
client: Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetch_heartbeat(&self, endpoints: &UptimeKumaEndpoints) -> Result<HeartbeatResponse> {
|
||||
let response = self.client.get(endpoints.heartbeat_url()).send()?;
|
||||
|
||||
if response.status().is_success() {
|
||||
let json_text = response.text()?;
|
||||
data::heartbeat::parse_response(&json_text)
|
||||
} else {
|
||||
return Err(anyhow::anyhow!(response.status()));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fetch_status_page(&self, endpoints: &UptimeKumaEndpoints) -> Result<StatusPageResponse> {
|
||||
let response = self.client.get(endpoints.status_page_url()).send()?;
|
||||
if response.status().is_success() {
|
||||
let json_text = response.text()?;
|
||||
data::status_page::parse_response(&json_text)
|
||||
} else {
|
||||
return Err(anyhow::anyhow!(response.status()));
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/api/endpoints.rs
Normal file
25
src/api/endpoints.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use url::Url;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UptimeKumaEndpoints {
|
||||
base_url: Url,
|
||||
slug: String,
|
||||
}
|
||||
|
||||
impl UptimeKumaEndpoints {
|
||||
pub fn new(base_url: &str, slug: &str) -> Result<Self, url::ParseError> {
|
||||
let base_url = Url::parse(base_url.trim_end_matches("/"))?;
|
||||
Ok(Self {
|
||||
base_url,
|
||||
slug: slug.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn heartbeat_url(&self) -> String {
|
||||
format!("{}api/status-page/heartbeat/{}", self.base_url, self.slug)
|
||||
}
|
||||
|
||||
pub fn status_page_url(&self) -> String {
|
||||
format!("{}api/status-page/{}", self.base_url, self.slug)
|
||||
}
|
||||
}
|
||||
5
src/api/mod.rs
Normal file
5
src/api/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
pub mod client;
|
||||
pub mod endpoints;
|
||||
|
||||
pub use client::UptimeKumaClient;
|
||||
pub use endpoints::UptimeKumaEndpoints;
|
||||
|
|
@ -1,20 +1,8 @@
|
|||
use crate::data::heartbeat;
|
||||
use crate::data::heartbeat::model::{HeartbeatEntry, HeartbeatResponse, UptimeData};
|
||||
use crate::data::status_page::model::{MonitorInfo, StatusPageResponse};
|
||||
use crate::core::models::{UnifiedData, UnifiedMonitorData};
|
||||
use crate::data::heartbeat::model::{HeartbeatResponse};
|
||||
use crate::data::status_page::model::{StatusPageResponse};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedMonitorData {
|
||||
pub monitor_info: MonitorInfo,
|
||||
pub heartbeats: Vec<HeartbeatEntry>,
|
||||
pub uptime_data: Option<UptimeData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedData {
|
||||
pub status_page_title: String,
|
||||
pub status_page_description: Option<String>,
|
||||
pub monitors: Vec<UnifiedMonitorData>,
|
||||
}
|
||||
pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatResponse) -> UnifiedData {
|
||||
let mut monitors = Vec::new();
|
||||
|
||||
5
src/core/mod.rs
Normal file
5
src/core/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
pub mod data;
|
||||
pub mod models;
|
||||
|
||||
pub use data::unify_data;
|
||||
pub use models::{UnifiedData, UnifiedMonitorData};
|
||||
16
src/core/models.rs
Normal file
16
src/core/models.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
use crate::data::heartbeat::model::{HeartbeatEntry, HeartbeatResponse, UptimeData};
|
||||
use crate::data::status_page::model::{MonitorInfo, StatusPageResponse};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedMonitorData {
|
||||
pub monitor_info: MonitorInfo,
|
||||
pub heartbeats: Vec<HeartbeatEntry>,
|
||||
pub uptime_data: Option<UptimeData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedData {
|
||||
pub status_page_title: String,
|
||||
pub status_page_description: Option<String>,
|
||||
pub monitors: Vec<UnifiedMonitorData>,
|
||||
}
|
||||
|
|
@ -1,4 +1,2 @@
|
|||
pub mod unified;
|
||||
pub use unified::model::unify_data;
|
||||
pub mod heartbeat;
|
||||
pub mod status_page;
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
pub mod model;
|
||||
36
src/main.rs
36
src/main.rs
|
|
@ -1,11 +1,13 @@
|
|||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use reqwest::blocking::Client;
|
||||
use std::result::Result::Ok;
|
||||
mod i18n;
|
||||
use i18n::init_locales;
|
||||
|
||||
use crate::data::unify_data;
|
||||
mod api;
|
||||
use crate::core::unify_data;
|
||||
use api::UptimeKumaClient;
|
||||
use api::UptimeKumaEndpoints;
|
||||
mod core;
|
||||
mod data;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
|
|
@ -21,33 +23,13 @@ struct Args {
|
|||
fn main() -> Result<()> {
|
||||
init_locales()?;
|
||||
let args = Args::parse();
|
||||
let base_url = args.base_url.trim_end_matches("/");
|
||||
|
||||
let heartbeat_url = format!("{}/api/status-page/heartbeat/{}", base_url, args.slug);
|
||||
let status_page_url = format!("{}/api/status-page/{}", base_url, args.slug);
|
||||
let client = UptimeKumaClient::new();
|
||||
let endpoints = UptimeKumaEndpoints::new(&args.base_url, &args.slug)?;
|
||||
|
||||
let client = Client::new();
|
||||
let heartbeat_data = client.fetch_heartbeat(&endpoints)?;
|
||||
|
||||
println!("Heartbeat URL: {}", heartbeat_url);
|
||||
|
||||
let heartbeat_response = client.get(heartbeat_url).send()?;
|
||||
|
||||
let heartbeat_data = if heartbeat_response.status().is_success() {
|
||||
let json_text = heartbeat_response.text()?;
|
||||
data::heartbeat::parse_response(&json_text)?
|
||||
} else {
|
||||
return Err(anyhow::anyhow!(heartbeat_response.status()));
|
||||
};
|
||||
|
||||
println!("Status Page URL: {}", status_page_url);
|
||||
|
||||
let status_page_response = client.get(status_page_url).send()?;
|
||||
let status_page_data = if status_page_response.status().is_success() {
|
||||
let json_text = status_page_response.text()?;
|
||||
data::status_page::parse_response(&json_text)?
|
||||
} else {
|
||||
return Err(anyhow::anyhow!(status_page_response.status()));
|
||||
};
|
||||
let status_page_data = client.fetch_status_page(&endpoints)?;
|
||||
|
||||
let unified_data = unify_data(&status_page_data, &heartbeat_data);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue