Using heartbeat api instead

This commit is contained in:
Marco De Araujo 2025-12-22 10:51:58 -04:00
parent 4cedb17b60
commit d024280878
10 changed files with 319 additions and 40 deletions

View file

@ -1,58 +1,42 @@
use std::str::FromStr;
use anyhow::{Ok, Result};
use anyhow::Result;
use clap::Parser;
use fluent_templates::{Loader, static_loader};
use reqwest::blocking::Client;
use unic_langid::LanguageIdentifier;
static_loader! {
static LOCALES = {
locales: "./src/locales",
fallback_language: "pt-BR",
customise: |bundle| bundle.set_use_isolating(false),
};
}
use std::result::Result::Ok;
mod i18n;
use i18n::{init_locales, t};
mod heartbeat;
#[derive(Debug, Parser)]
#[command(author, version, about)]
struct Args {
#[arg(short, long, env = "UPTIME_KUMA_URL")]
url: Option<String>,
base_url: String,
#[arg(short, long, env = "UPTIME_KUMA_API_KEY")]
api_key: Option<String>,
}
fn get_sys_locale() -> LanguageIdentifier {
let sys_lang = sys_locale::get_locale().unwrap_or_else(|| String::from("pt-BR"));
LanguageIdentifier::from_str(&sys_lang).expect("Invalid language")
}
fn t(key: &str) -> String {
LOCALES.lookup(&get_sys_locale(), key)
#[arg(short, long, env = "STATUS_PAGE_SLUG")]
slug: String,
}
fn main() -> Result<()> {
init_locales()?;
let args = Args::parse();
let url = args
.url
.as_deref()
.ok_or_else(|| anyhow::anyhow!("{}", t("missing_url")))?;
let api_key = args
.api_key
.as_deref()
.ok_or_else(|| anyhow::anyhow!("{}", t("missing_api_key")))?;
let heartbeat_url = format!(
"{}/api/status-page/heartbeat/{}",
args.base_url.trim_end_matches("/"),
args.slug
);
let client = Client::new();
let response = client.get(url).basic_auth("", Some(api_key)).send()?;
println!("{}", heartbeat_url);
let response = client.get(heartbeat_url).send()?;
if response.status().is_success() {
let metrics = response.text()?;
println!("{}", t("success"));
println!("{}", t("metrics_preview"));
println!("{}", &metrics[..200.min(metrics.len())]);
let json_text = response.text()?;
match heartbeat::parse_response(&json_text) {
Ok(data) => println!("moises: {}", data.uptime_data[0].get_perc_formated()),
Err(e) => println!("{}", e),
}
} else {
println!("{}", response.status());
println!("{}: {}", t("Response"), response.text()?);