blob: 059909fae0a691471647feb75234eb267d040053 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
use std::{env, collections::HashMap, fs::read_to_string, process::{exit, Command}};
pub fn load_config() -> HashMap<String, String> {
let config_path = env::var("CONFIG_PATH").unwrap_or_else(|_| String::from("config"));
let config = match read_to_string(&config_path) {
Ok(data) => data,
Err(err) => {
eprintln!("cannot load '{config_path}': {err}");
exit(1);
},
};
let mut map = HashMap::new();
let lines = config.split("\n").into_iter();
for line in lines {
let mut parts = line.trim().split(" ");
let Some(route) = parts.next() else { continue };
let Some(script) = parts.next() else { continue };
println!("adding entry {route} => {script}");
map.insert(route.to_owned(), script.to_owned());
}
map
}
pub fn handle_script(script: &str, body: Option<&String>) -> Result<String, String> {
let mut command = Command::new(script);
if let Some(body) = body {
command.args([body]);
}
let output = match command.output() {
Ok(o) => o,
Err(err) => return Err(format!("{err}")),
};
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
|