From ad46eae8b31e812bf2b29fa1e020ae0fa1b50481 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Tue, 10 Sep 2024 10:19:11 +0100 Subject: [PATCH] Add next config functionality --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 50 +++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 53e940f..09f1084 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,6 +102,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "anyhow" +version = "1.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10f00e1f6e58a40e807377c75c6a7f97bf9044fab57816f2414e6f5f4499d7b8" + [[package]] name = "ascii" version = "1.1.0" @@ -707,6 +713,7 @@ dependencies = [ name = "monitor-controller" version = "0.1.0" dependencies = [ + "anyhow", "clap", "reqwest", "rouille", diff --git a/Cargo.toml b/Cargo.toml index 7b9834d..17e93a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" clap = "4.5.17" rouille = "3.6.2" reqwest = { version = "0.11", features = ["blocking"] } +anyhow = "1.0.87" diff --git a/src/main.rs b/src/main.rs index a829348..4ed7d9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use std::{ sync::{Arc, Mutex}, }; +use anyhow::{anyhow, Result}; use clap::{Arg, ArgAction, Command}; use rouille::Response; @@ -27,6 +28,21 @@ fn get_config(path: &Path) -> Vec { return c; } +fn switch_to_config(base_path: &String, p: String) -> Result<()> { + let res = std::process::Command::new("ln") + .arg("-sf") + .arg(base_path.clone().to_owned() + "/" + &p + ".conf") + .arg(base_path.clone().to_owned() + "/monitors.conf") + .spawn(); + + if res.is_err() { + println!("Well somwthing went very wrong"); + return Err(anyhow!("Something went very wrong")); + } + + return Ok(()); +} + fn main() { println!("Hello, world!"); @@ -109,6 +125,8 @@ fn main() { .expect("Failed to change monitor on boot"); } + let config_ptr: Arc> = Arc::new(Mutex::new(0)); + rouille::start_server("0.0.0.0:11111", move |request| { if request.url().eq("/reload") { println!("Reloading config!"); @@ -118,6 +136,22 @@ fn main() { *c = get_config(&config_path); return Response::text("reloaded!"); + } else if request.url().eq("/next_config") { + println!("Switiching to the next config"); + let c_ptr = config_ptr.clone(); + let mut c_ptr = c_ptr.lock().ok().unwrap(); + + let c = config.clone(); + let c = c.lock().ok().unwrap(); + + let ptr = (*c_ptr + 1) % c.len(); + + if switch_to_config(&base_path, c[ptr].clone()).is_err() { + return Response::text("Something went very wrong!").with_status_code(500); + } + + *c_ptr = ptr; + return Response::text("Change it!"); } if !request.url().starts_with("/switch_to/") { @@ -137,17 +171,15 @@ fn main() { .with_status_code(404); } - let res = std::process::Command::new("ln") - .arg("-sf") - .arg(base_path.clone().to_owned() + "/" + &u.to_string() + ".conf") - .arg(base_path.clone().to_owned() + "/monitors.conf") - .spawn(); - - if res.is_err() { - println!("Well somwthing went very wrong"); - return Response::text("Something went very wrong").with_status_code(500); + if switch_to_config(&base_path, u.to_string()).is_err() { + return Response::text("Something went very wrong!").with_status_code(500); } + let c_ptr = config_ptr.clone(); + let mut c_ptr = c_ptr.lock().ok().unwrap(); + + *c_ptr = c.iter().position(|d| d.eq(&u)).unwrap(); + return Response::text("Chaged it"); }) } else {