about summary refs log tree commit diff stats
path: root/src/main.rs
blob: f4e4b1c8ac79ad109d0cff51254dd5c1872949c0 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
pub mod model;

use clap::Parser;
use serde::{Deserialize, Serialize};

use tokio::fs::read_to_string;

use std::{
    net::IpAddr,
    path::PathBuf,
    sync::Arc,
};

use crate::model::{Service, ServiceConf};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    #[arg(
        short,
        long,
        value_name = "FILE",
        help = "config file override",
        default_value = "salaryman.toml"
    )]
    config: PathBuf,
    #[arg(
        short,
        long,
        value_name = "ADDR",
        help = "IP address to bind API to",
        default_value = "127.0.0.1"
    )]
    address: IpAddr,
    #[arg(
        short,
        long,
        value_name = "PORT",
        help = "TCP Port to bind API to",
        default_value = "3080"
    )]
    port: u16,
}

#[derive(Serialize, Deserialize, Debug)]
struct Config {
    address: Option<IpAddr>,
    port: Option<u16>,
    service: Vec<ServiceConf>,
}
impl Config {
    fn new() -> Self {
        Self {
            address: None,
            port: None,
            service: Vec::new(),
        }
    }
}

async fn load_config(file: &PathBuf) -> Config {
    let s: String = match read_to_string(file).await {
        Ok(s) => s,
        Err(_) => String::new(),
    };
    match toml::from_str(s.as_str()) {
        Ok(c) => c,
        Err(_) => Config::new(),
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();
    let conf: Config = load_config(&args.config).await;
    let mut services: Vec<Service> = Vec::new();
    for i in 0..conf.service.len() {
        services.push(Service::from_conf(conf.service[i].clone()));
        if conf.service[i].autostart {
            services[i].start()?;
        }
    }
    for i in 0..services.len() {
        println!("loop -1");
        if let Ok(s) = services[i].stdout() {
            for line in s.lines() {
                println!("STDOUT :: [{}] :: {}", services[i].config.name, line);
            }
        }
        if let Ok(s) = services[i].stderr() {
            for line in s.lines() {
                println!("STDERR :: [{}] :: {}", services[i].config.name, line);
            }
        }
    }
    for e in 0..100 {
        println!("loop {e}");
        for i in 0..services.len() {
            if let Ok(s) = services[i].stdout() {
                for line in s.lines() {
                    println!("STDOUT :: [{}] :: {}", services[i].config.name, line);
                }
            }
            if let Ok(s) = services[i].stderr() {
                for line in s.lines() {
                    println!("STDERR :: [{}] :: {}", services[i].config.name, line);
                }
            }
        };
    }
    for mut service in services {
        match service.stop() {
            Ok(_) => println!("lol it was killed"),
            Err(_) => println!("it either didn't exist, or failed to kill"),
        }
    }
    Ok(())
}