ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/salaryman/trunk/src/main.rs
Revision: 8
Committed: Tue Jul 8 01:49:14 2025 UTC (3 months ago) by yuzu
File size: 2445 byte(s)
Log Message:
go fully async

File Contents

# User Rev Content
1 yuzu 5 use clap::Parser;
2 yuzu 6 use serde::{Deserialize, Serialize};
3 yuzu 4
4 yuzu 5 use tokio::fs::read_to_string;
5 yuzu 2
6 yuzu 8 use std::{net::IpAddr, path::PathBuf};
7 yuzu 5
8 yuzu 8 use salaryman::model::{Service, ServiceConf};
9 yuzu 7
10     #[derive(Parser, Debug)]
11 yuzu 5 #[command(version, about, long_about = None)]
12     struct Args {
13 yuzu 7 #[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 yuzu 1 }
38 yuzu 2
39 yuzu 6 #[derive(Serialize, Deserialize, Debug)]
40 yuzu 5 struct Config {
41     address: Option<IpAddr>,
42     port: Option<u16>,
43 yuzu 7 service: Vec<ServiceConf>,
44 yuzu 5 }
45     impl Config {
46     fn new() -> Self {
47     Self {
48     address: None,
49     port: None,
50     service: Vec::new(),
51     }
52     }
53     }
54    
55 yuzu 7 async fn load_config(file: &PathBuf) -> Config {
56 yuzu 6 let s: String = match read_to_string(file).await {
57     Ok(s) => s,
58     Err(_) => String::new(),
59     };
60     match toml::from_str(s.as_str()) {
61     Ok(c) => c,
62     Err(_) => Config::new(),
63     }
64 yuzu 5 }
65    
66 yuzu 4 #[tokio::main]
67     async fn main() -> Result<(), Box<dyn std::error::Error>> {
68 yuzu 5 let args = Args::parse();
69 yuzu 7 let conf: Config = load_config(&args.config).await;
70     let mut services: Vec<Service> = Vec::new();
71     for i in 0..conf.service.len() {
72 yuzu 8 services.push(Service::from_conf(&conf.service[i]));
73 yuzu 7 if conf.service[i].autostart {
74 yuzu 8 services[i].start().await?;
75 yuzu 7 }
76     }
77 yuzu 8 let mut outs: Vec<(String, tokio::sync::mpsc::Receiver<String>)> = Vec::new();
78 yuzu 7 for i in 0..services.len() {
79 yuzu 8 if services[i].started().await {
80     outs.push((services[i].name().await, services[i].scan_stdout().await?));
81 yuzu 7 }
82 yuzu 8 }
83     for _i in 0..100 {
84     for out in 0..outs.len() {
85     if let Some(s) = outs[out].1.recv().await {
86     println!("got line from {} :: {}", outs[out].0, s);
87 yuzu 7 }
88     }
89     }
90     for mut service in services {
91 yuzu 8 match service.stop().await {
92 yuzu 7 Ok(_) => println!("lol it was killed"),
93     Err(_) => println!("it either didn't exist, or failed to kill"),
94     }
95     }
96 yuzu 2 Ok(())
97     }