Add next config functionality
This commit is contained in:
parent
c3a67f4928
commit
ad46eae8b3
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -102,6 +102,12 @@ dependencies = [
|
|||||||
"windows-sys 0.52.0",
|
"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]]
|
[[package]]
|
||||||
name = "ascii"
|
name = "ascii"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@ -707,6 +713,7 @@ dependencies = [
|
|||||||
name = "monitor-controller"
|
name = "monitor-controller"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rouille",
|
"rouille",
|
||||||
|
@ -7,3 +7,4 @@ edition = "2021"
|
|||||||
clap = "4.5.17"
|
clap = "4.5.17"
|
||||||
rouille = "3.6.2"
|
rouille = "3.6.2"
|
||||||
reqwest = { version = "0.11", features = ["blocking"] }
|
reqwest = { version = "0.11", features = ["blocking"] }
|
||||||
|
anyhow = "1.0.87"
|
||||||
|
50
src/main.rs
50
src/main.rs
@ -7,6 +7,7 @@ use std::{
|
|||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
use clap::{Arg, ArgAction, Command};
|
use clap::{Arg, ArgAction, Command};
|
||||||
use rouille::Response;
|
use rouille::Response;
|
||||||
|
|
||||||
@ -27,6 +28,21 @@ fn get_config(path: &Path) -> Vec<String> {
|
|||||||
return c;
|
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() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
@ -109,6 +125,8 @@ fn main() {
|
|||||||
.expect("Failed to change monitor on boot");
|
.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| {
|
rouille::start_server("0.0.0.0:11111", move |request| {
|
||||||
if request.url().eq("/reload") {
|
if request.url().eq("/reload") {
|
||||||
println!("Reloading config!");
|
println!("Reloading config!");
|
||||||
@ -118,6 +136,22 @@ fn main() {
|
|||||||
*c = get_config(&config_path);
|
*c = get_config(&config_path);
|
||||||
|
|
||||||
return Response::text("reloaded!");
|
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/") {
|
if !request.url().starts_with("/switch_to/") {
|
||||||
@ -137,17 +171,15 @@ fn main() {
|
|||||||
.with_status_code(404);
|
.with_status_code(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = std::process::Command::new("ln")
|
if switch_to_config(&base_path, u.to_string()).is_err() {
|
||||||
.arg("-sf")
|
return Response::text("Something went very wrong!").with_status_code(500);
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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");
|
return Response::text("Chaged it");
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user