about summary refs log tree commit diff stats
path: root/src/server/main.rs
blob: 75571459a635643e9dbaccdc832a7c0b03f3e462 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
mod context;
mod endpoints;

use clap::Parser;
use dropshot::{ApiDescription, ConfigDropshot, ConfigLogging, ConfigLoggingLevel, ServerBuilder};
use salaryman::service::{Service, ServiceConf};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tokio::{fs::read_to_string, sync::RwLock};

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

use crate::context::{SalarymanDContext, SalarymanService};
use crate::endpoints::{
    endpoint_get_config, endpoint_get_config_save, endpoint_get_service, endpoint_get_services,
    endpoint_post_service, endpoint_post_stdin, endpoint_put_config, endpoint_restart_service,
    endpoint_start_service, endpoint_stop_service,
};

#[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, JsonSchema, Clone, Debug)]
pub struct User {
    pub username: String,
    pub token: String,
}

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

async fn load_config(file: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
    let s: String = match read_to_string(file).await {
        Ok(s) => s,
        Err(_) => {
            return Err(Box::new(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "cannot find config file",
            )));
        }
    };
    match toml::from_str(s.as_str()) {
        Ok(c) => Ok(c),
        Err(_) => Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::Other,
            "unable to parse config file",
        ))),
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();
    let conf: Config = load_config(&args.config).await?;
    let addr = if let Some(addr) = conf.address {
        addr
    } else {
        args.address
    };
    let port = if let Some(port) = conf.port {
        port
    } else {
        args.port
    };
    let bind = SocketAddr::new(addr, port);
    let services: RwLock<Vec<Arc<SalarymanService>>> = RwLock::new(Vec::new());
    for i in 0..conf.service.len() {
        let mut lock = services.write().await;
        lock.push(Arc::new(SalarymanService::from_parts(
            conf.service[i].clone(),
            Arc::new(RwLock::new(Service::from_conf(&conf.service[i]))),
        )));
        drop(lock);
    }
    let lock = services.write().await;
    for i in 0..lock.len() {
        if lock[i].config.autostart {
            let mut l = lock[i].service.write().await;
            l.start().await?;
            l.scan_stdout().await?;
            l.scan_stderr().await?;
            drop(l);
        }
    }
    drop(lock);
    let log_conf = ConfigLogging::StderrTerminal {
        level: ConfigLoggingLevel::Info,
    };
    let log = log_conf.to_logger("smd")?;
    let ctx = Arc::new(SalarymanDContext::from_parts(
        services,
        args.config,
        Arc::new(RwLock::new(conf)),
    ));
    let config = ConfigDropshot {
        bind_address: bind,
        ..Default::default()
    };
    let mut api = ApiDescription::new();
    api.register(endpoint_get_services)?;
    api.register(endpoint_get_service)?;
    api.register(endpoint_start_service)?;
    api.register(endpoint_stop_service)?;
    api.register(endpoint_restart_service)?;
    api.register(endpoint_post_stdin)?;
    api.register(endpoint_post_service)?;
    api.register(endpoint_get_config)?;
    api.register(endpoint_put_config)?;
    api.register(endpoint_get_config_save)?;
    api.openapi("Salaryman", semver::Version::new(1, 0, 0))
        .write(&mut std::io::stdout())?;
    let server = ServerBuilder::new(api, ctx.clone(), log)
        .config(config)
        .start()?;
    server.await?;
    Ok(())
}