From 6d434ae4a41a088c8dd9893537cc7abb872bd1fb Mon Sep 17 00:00:00 2001 From: George Kaklamanos Date: Tue, 23 May 2023 19:17:25 +0300 Subject: [PATCH] Serve the current state using rocket --- Cargo.toml | 1 + src/main.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b4e47a1..51beac0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" clap = { version = "4.2.7", features = ["cargo", "derive"] } regex = "1.8.1" reqwest = { version = "0.11.17", features = ["blocking", "json"] } +rocket = "=0.5.0-rc.3" serde_json = "1.0.96" spaceapi = "0.9.0" diff --git a/src/main.rs b/src/main.rs index f1a9761..96827d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,10 +17,14 @@ struct Cli { use std::sync::{Arc, Mutex}; type Body = Arc>; -fn main() { +use rocket::{get, launch, routes, State}; + +#[launch] +fn rocket() -> _ { let cli = Cli::parse(); let body: Body = Arc::new(Mutex::new(String::new())); + let body_state = body.clone(); std::thread::spawn(move || loop { let str = client::get_prometheus_string(cli.url.clone()); let mut lock = body.lock().unwrap(); @@ -29,4 +33,13 @@ fn main() { std::thread::sleep(std::time::Duration::from_millis(cli.delay * 1000)); }); + + rocket::build() + .manage(body_state) + .mount("/prometheus", routes![prometheus]) +} + +#[get("/")] +fn prometheus(body: &State) -> String { + format!("{}", body.lock().unwrap()) }