mirror of
https://github.com/Noratrieb/uptime.git
synced 2026-01-14 08:35:07 +01:00
start
This commit is contained in:
parent
9b6ab3c83e
commit
24abf6d98c
6 changed files with 1417 additions and 0 deletions
1345
Cargo.lock
generated
1345
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -6,3 +6,12 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
axum = "0.6.20"
|
||||
chrono = "0.4.27"
|
||||
eyre = "0.6.8"
|
||||
http = "0.2.9"
|
||||
reqwest = { version = "0.11.20", default-features = false, features = ["rustls-tls"] }
|
||||
rusqlite = { version = "0.29.0", features = ["bundled"] }
|
||||
tracing = "0.1.37"
|
||||
tracing-subscriber = "0.3.17"
|
||||
url = "2.4.1"
|
||||
|
|
|
|||
54
src/client.rs
Normal file
54
src/client.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use chrono::Utc;
|
||||
use eyre::{bail, Context, Result};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::config::WebsiteConfig;
|
||||
|
||||
pub struct Client {
|
||||
websites: Vec<WebsiteConfig>,
|
||||
req: reqwest::Client,
|
||||
}
|
||||
|
||||
pub struct Results {
|
||||
pub states: HashMap<String, CheckResult>,
|
||||
}
|
||||
|
||||
pub struct CheckResult {
|
||||
pub time: chrono::DateTime<Utc>,
|
||||
pub state: CheckState,
|
||||
}
|
||||
|
||||
pub enum CheckState {
|
||||
Ok,
|
||||
NotOk,
|
||||
}
|
||||
|
||||
pub async fn do_checks(client: &Client) -> Results {
|
||||
let mut states = HashMap::new();
|
||||
for website in &client.websites {
|
||||
let check_result = make_request(&client.req, website).await;
|
||||
states.insert(website.name.clone(), check_result);
|
||||
}
|
||||
|
||||
Results { states }
|
||||
}
|
||||
|
||||
async fn make_request(client: &reqwest::Client, website: &WebsiteConfig) -> CheckResult {
|
||||
let time = Utc::now();
|
||||
let result = client.get(website.url.clone()).send().await;
|
||||
|
||||
match result {
|
||||
Ok(res) => CheckResult {
|
||||
time,
|
||||
state: if res.status().is_success() {
|
||||
CheckState::Ok
|
||||
} else {
|
||||
CheckState::NotOk
|
||||
},
|
||||
},
|
||||
Err(err) => CheckResult {
|
||||
time,
|
||||
state: CheckState::NotOk,
|
||||
},
|
||||
}
|
||||
}
|
||||
6
src/config.rs
Normal file
6
src/config.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
use url::Url;
|
||||
|
||||
pub struct WebsiteConfig {
|
||||
pub name: String,
|
||||
pub url: Url,
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
mod client;
|
||||
mod config;
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
fn main() {
|
||||
tracing_subscriber::fmt().init();
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue