ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/salaryman/trunk/src/server/main.rs
Revision: 11
Committed: Wed Jul 9 05:14:23 2025 UTC (3 months ago) by yuzu
Original Path: trunk/src/smd/main.rs
File size: 2566 byte(s)
Log Message:
reorganize

File Contents

# User Rev Content
1 yuzu 11 mod endpoints;
2    
3 yuzu 9 use clap::Parser;
4     use serde::{Deserialize, Serialize};
5     use tokio::fs::read_to_string;
6 yuzu 11 use salaryman::service::{Service, ServiceConf};
7 yuzu 9
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 yuzu 11 /*
46 yuzu 9 impl Config {
47     fn new() -> Self {
48     Self {
49     address: None,
50     port: None,
51     service: Vec::new(),
52     }
53     }
54     }
55 yuzu 11 */
56 yuzu 9
57 yuzu 11 async fn load_config(file: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
58 yuzu 9 let s: String = match read_to_string(file).await {
59     Ok(s) => s,
60 yuzu 11 Err(_) => return Err(Box::new(std::io::Error::new(std::io::ErrorKind::NotFound, "cannot find config file"))),
61 yuzu 9 };
62     match toml::from_str(s.as_str()) {
63 yuzu 11 Ok(c) => Ok(c),
64     Err(_) => Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "unable to parse config file"))),
65 yuzu 9 }
66     }
67    
68     #[tokio::main]
69     async fn main() -> Result<(), Box<dyn std::error::Error>> {
70     let args = Args::parse();
71 yuzu 11 let conf: Config = load_config(&args.config).await?;
72 yuzu 9 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 yuzu 11