ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/salaryman/trunk/src/main.rs
Revision: 7
Committed: Wed Jun 4 04:45:58 2025 UTC (4 months ago) by yuzu
File size: 2970 byte(s)
Log Message:
Work on Service::stdout() and Service::stderr()

File Contents

# Content
1 pub mod model;
2
3 use clap::Parser;
4 use serde::{Deserialize, Serialize};
5
6 use tokio::fs::read_to_string;
7
8 use std::{
9 net::IpAddr,
10 path::PathBuf,
11 sync::Arc,
12 };
13
14 use crate::model::{Service, ServiceConf};
15
16 #[derive(Parser, Debug)]
17 #[command(version, about, long_about = None)]
18 struct Args {
19 #[arg(
20 short,
21 long,
22 value_name = "FILE",
23 help = "config file override",
24 default_value = "salaryman.toml"
25 )]
26 config: PathBuf,
27 #[arg(
28 short,
29 long,
30 value_name = "ADDR",
31 help = "IP address to bind API to",
32 default_value = "127.0.0.1"
33 )]
34 address: IpAddr,
35 #[arg(
36 short,
37 long,
38 value_name = "PORT",
39 help = "TCP Port to bind API to",
40 default_value = "3080"
41 )]
42 port: u16,
43 }
44
45 #[derive(Serialize, Deserialize, Debug)]
46 struct Config {
47 address: Option<IpAddr>,
48 port: Option<u16>,
49 service: Vec<ServiceConf>,
50 }
51 impl Config {
52 fn new() -> Self {
53 Self {
54 address: None,
55 port: None,
56 service: Vec::new(),
57 }
58 }
59 }
60
61 async fn load_config(file: &PathBuf) -> Config {
62 let s: String = match read_to_string(file).await {
63 Ok(s) => s,
64 Err(_) => String::new(),
65 };
66 match toml::from_str(s.as_str()) {
67 Ok(c) => c,
68 Err(_) => Config::new(),
69 }
70 }
71
72 #[tokio::main]
73 async fn main() -> Result<(), Box<dyn std::error::Error>> {
74 let args = Args::parse();
75 let conf: Config = load_config(&args.config).await;
76 let mut services: Vec<Service> = Vec::new();
77 for i in 0..conf.service.len() {
78 services.push(Service::from_conf(conf.service[i].clone()));
79 if conf.service[i].autostart {
80 services[i].start()?;
81 }
82 }
83 for i in 0..services.len() {
84 println!("loop -1");
85 if let Ok(s) = services[i].stdout() {
86 for line in s.lines() {
87 println!("STDOUT :: [{}] :: {}", services[i].config.name, line);
88 }
89 }
90 if let Ok(s) = services[i].stderr() {
91 for line in s.lines() {
92 println!("STDERR :: [{}] :: {}", services[i].config.name, line);
93 }
94 }
95 }
96 for e in 0..100 {
97 println!("loop {e}");
98 for i in 0..services.len() {
99 if let Ok(s) = services[i].stdout() {
100 for line in s.lines() {
101 println!("STDOUT :: [{}] :: {}", services[i].config.name, line);
102 }
103 }
104 if let Ok(s) = services[i].stderr() {
105 for line in s.lines() {
106 println!("STDERR :: [{}] :: {}", services[i].config.name, line);
107 }
108 }
109 };
110 }
111 for mut service in services {
112 match service.stop() {
113 Ok(_) => println!("lol it was killed"),
114 Err(_) => println!("it either didn't exist, or failed to kill"),
115 }
116 }
117 Ok(())
118 }