Serve the current state using rocket

This commit is contained in:
George Kaklamanos 2023-05-23 19:17:25 +03:00
parent 42bdc0361d
commit 6d434ae4a4
2 changed files with 15 additions and 1 deletions

View file

@ -7,5 +7,6 @@ edition = "2021"
clap = { version = "4.2.7", features = ["cargo", "derive"] } clap = { version = "4.2.7", features = ["cargo", "derive"] }
regex = "1.8.1" regex = "1.8.1"
reqwest = { version = "0.11.17", features = ["blocking", "json"] } reqwest = { version = "0.11.17", features = ["blocking", "json"] }
rocket = "=0.5.0-rc.3"
serde_json = "1.0.96" serde_json = "1.0.96"
spaceapi = "0.9.0" spaceapi = "0.9.0"

View file

@ -17,10 +17,14 @@ struct Cli {
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
type Body = Arc<Mutex<String>>; type Body = Arc<Mutex<String>>;
fn main() { use rocket::{get, launch, routes, State};
#[launch]
fn rocket() -> _ {
let cli = Cli::parse(); let cli = Cli::parse();
let body: Body = Arc::new(Mutex::new(String::new())); let body: Body = Arc::new(Mutex::new(String::new()));
let body_state = body.clone();
std::thread::spawn(move || loop { std::thread::spawn(move || loop {
let str = client::get_prometheus_string(cli.url.clone()); let str = client::get_prometheus_string(cli.url.clone());
let mut lock = body.lock().unwrap(); let mut lock = body.lock().unwrap();
@ -29,4 +33,13 @@ fn main() {
std::thread::sleep(std::time::Duration::from_millis(cli.delay * 1000)); 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<Body>) -> String {
format!("{}", body.lock().unwrap())
} }