ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/salaryman/trunk/src/bin/smd.rs
Revision: 9
Committed: Tue Jul 8 07:12:59 2025 UTC (3 months ago) by yuzu
File size: 2338 byte(s)
Log Message:
working proof of concept -- minecraft

File Contents

# Content
1 use clap::Parser;
2 use serde::{Deserialize, Serialize};
3
4 use tokio::fs::read_to_string;
5
6 use std::{net::IpAddr, path::PathBuf};
7
8 use salaryman::model::{Service, ServiceConf};
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 impl Config {
46 fn new() -> Self {
47 Self {
48 address: None,
49 port: None,
50 service: Vec::new(),
51 }
52 }
53 }
54
55 async fn load_config(file: &PathBuf) -> Config {
56 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 }
65
66 #[tokio::main]
67 async fn main() -> Result<(), Box<dyn std::error::Error>> {
68 let args = Args::parse();
69 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 services.push(Service::from_conf(&conf.service[i]));
73 if conf.service[i].autostart {
74 services[i].start().await?;
75 services[i].scan_stdout().await?;
76 services[i].scan_stderr().await?;
77 }
78 }
79 tokio::time::sleep(std::time::Duration::from_secs(60)).await;
80 println!("trying to write to stdin!");
81 for i in 0..services.len() {
82 services[i].write_stdin("stop\n".into()).await?;
83 }
84 tokio::time::sleep(std::time::Duration::from_secs(30)).await;
85 for mut service in services {
86 match service.stop().await {
87 Ok(_) => println!("lol it was killed"),
88 Err(_) => println!("it either didn't exist, or failed to kill"),
89 }
90 }
91 Ok(())
92 }