1 |
mod endpoints; |
2 |
|
3 |
use clap::Parser; |
4 |
use serde::{Deserialize, Serialize}; |
5 |
use tokio::fs::read_to_string; |
6 |
use salaryman::service::{Service, ServiceConf}; |
7 |
|
8 |
use std::{net::IpAddr, path::PathBuf}; |
9 |
|
10 |
#[derive(Parser, Debug)] |
11 |
#[command(version, about, long_about = None)] |
12 |
struct Args { |
13 |
#[arg( |
14 |
short, |
15 |
long, |
16 |
value_name = "FILE", |
17 |
help = "config file override", |
18 |
default_value = "salaryman.toml" |
19 |
)] |
20 |
config: PathBuf, |
21 |
#[arg( |
22 |
short, |
23 |
long, |
24 |
value_name = "ADDR", |
25 |
help = "IP address to bind API to", |
26 |
default_value = "127.0.0.1" |
27 |
)] |
28 |
address: IpAddr, |
29 |
#[arg( |
30 |
short, |
31 |
long, |
32 |
value_name = "PORT", |
33 |
help = "TCP Port to bind API to", |
34 |
default_value = "3080" |
35 |
)] |
36 |
port: u16, |
37 |
} |
38 |
|
39 |
#[derive(Serialize, Deserialize, Debug)] |
40 |
struct Config { |
41 |
address: Option<IpAddr>, |
42 |
port: Option<u16>, |
43 |
service: Vec<ServiceConf>, |
44 |
} |
45 |
/* |
46 |
impl Config { |
47 |
fn new() -> Self { |
48 |
Self { |
49 |
address: None, |
50 |
port: None, |
51 |
service: Vec::new(), |
52 |
} |
53 |
} |
54 |
} |
55 |
*/ |
56 |
|
57 |
async fn load_config(file: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> { |
58 |
let s: String = match read_to_string(file).await { |
59 |
Ok(s) => s, |
60 |
Err(_) => return Err(Box::new(std::io::Error::new(std::io::ErrorKind::NotFound, "cannot find config file"))), |
61 |
}; |
62 |
match toml::from_str(s.as_str()) { |
63 |
Ok(c) => Ok(c), |
64 |
Err(_) => Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "unable to parse config file"))), |
65 |
} |
66 |
} |
67 |
|
68 |
#[tokio::main] |
69 |
async fn main() -> Result<(), Box<dyn std::error::Error>> { |
70 |
let args = Args::parse(); |
71 |
let conf: Config = load_config(&args.config).await?; |
72 |
let mut services: Vec<Service> = Vec::new(); |
73 |
for i in 0..conf.service.len() { |
74 |
services.push(Service::from_conf(&conf.service[i])); |
75 |
if conf.service[i].autostart { |
76 |
services[i].start().await?; |
77 |
services[i].scan_stdout().await?; |
78 |
services[i].scan_stderr().await?; |
79 |
} |
80 |
} |
81 |
tokio::time::sleep(std::time::Duration::from_secs(60)).await; |
82 |
println!("trying to write to stdin!"); |
83 |
for i in 0..services.len() { |
84 |
services[i].write_stdin("stop\n".into()).await?; |
85 |
} |
86 |
tokio::time::sleep(std::time::Duration::from_secs(30)).await; |
87 |
for mut service in services { |
88 |
match service.stop().await { |
89 |
Ok(_) => println!("lol it was killed"), |
90 |
Err(_) => println!("it either didn't exist, or failed to kill"), |
91 |
} |
92 |
} |
93 |
Ok(()) |
94 |
} |
95 |
|