Add next config functionality

This commit is contained in:
Andre Henriques 2024-09-10 10:19:11 +01:00
parent c3a67f4928
commit ad46eae8b3
3 changed files with 49 additions and 9 deletions

7
Cargo.lock generated
View File

@ -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",

View File

@ -7,3 +7,4 @@ edition = "2021"
clap = "4.5.17"
rouille = "3.6.2"
reqwest = { version = "0.11", features = ["blocking"] }
anyhow = "1.0.87"

View File

@ -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<String> {
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<Mutex<usize>> = 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 {