ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/salaryman/trunk/src/smd/main.rs
Revision: 12
Committed: Wed Jul 9 05:32:04 2025 UTC (3 months ago) by yuzu
File size: 2511 byte(s)
Log Message:
fix bug in service::Service; run fmt

File Contents

# User Rev Content
1 yuzu 11 mod endpoints;
2    
3 yuzu 9 use clap::Parser;
4 yuzu 12 use salaryman::service::{Service, ServiceConf};
5 yuzu 9 use serde::{Deserialize, Serialize};
6     use tokio::fs::read_to_string;
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 yuzu 11 async fn load_config(file: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
47 yuzu 9 let s: String = match read_to_string(file).await {
48     Ok(s) => s,
49 yuzu 12 Err(_) => {
50     return Err(Box::new(std::io::Error::new(
51     std::io::ErrorKind::NotFound,
52     "cannot find config file",
53     )));
54     }
55 yuzu 9 };
56     match toml::from_str(s.as_str()) {
57 yuzu 11 Ok(c) => Ok(c),
58 yuzu 12 Err(_) => Err(Box::new(std::io::Error::new(
59     std::io::ErrorKind::Other,
60     "unable to parse config file",
61     ))),
62 yuzu 9 }
63     }
64    
65     #[tokio::main]
66     async fn main() -> Result<(), Box<dyn std::error::Error>> {
67     let args = Args::parse();
68 yuzu 11 let conf: Config = load_config(&args.config).await?;
69 yuzu 9 let mut services: Vec<Service> = Vec::new();
70     for i in 0..conf.service.len() {
71     services.push(Service::from_conf(&conf.service[i]));
72     if conf.service[i].autostart {
73     services[i].start().await?;
74     services[i].scan_stdout().await?;
75     services[i].scan_stderr().await?;
76     }
77     }
78     tokio::time::sleep(std::time::Duration::from_secs(60)).await;
79     println!("trying to write to stdin!");
80     for i in 0..services.len() {
81     services[i].write_stdin("stop\n".into()).await?;
82     }
83     tokio::time::sleep(std::time::Duration::from_secs(30)).await;
84     for mut service in services {
85     match service.stop().await {
86     Ok(_) => println!("lol it was killed"),
87     Err(_) => println!("it either didn't exist, or failed to kill"),
88     }
89     }
90     Ok(())
91     }