aboutsummaryrefslogtreecommitdiffstats
path: root/teleterm/src/cmd/web.rs
blob: a9dc8304bfcdd1271e8b19c4e0fb8441ca3a64a7 (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
use crate::prelude::*;

#[derive(serde::Deserialize, Debug, Default)]
pub struct Config {
    #[serde(default)]
    web: crate::config::Web,

    #[serde(
        rename = "oauth",
        deserialize_with = "crate::config::oauth_configs",
        default
    )]
    oauth_configs: std::collections::HashMap<
        crate::protocol::AuthType,
        std::collections::HashMap<
            crate::protocol::AuthClient,
            crate::oauth::Config,
        >,
    >,
}

impl crate::config::Config for Config {
    fn merge_args<'a>(
        &mut self,
        matches: &clap::ArgMatches<'a>,
    ) -> Result<()> {
        self.web.merge_args(matches)
    }

    fn run(
        &self,
    ) -> Box<dyn futures::Future<Item = (), Error = Error> + Send> {
        Box::new(crate::web::Server::new(
            self.web.listen_address,
            self.web.public_address.clone(),
            self.web.server_address.clone(),
            self.web.allowed_login_methods.clone(),
            self.oauth_configs
                .iter()
                .filter_map(|(ty, configs)| {
                    configs.get(&crate::protocol::AuthClient::Web).map(
                        |config| {
                            let mut config = config.clone();
                            // TODO: tls
                            let url = url::Url::parse(&format!(
                                "http://{}/oauth/{}",
                                self.web.public_address,
                                ty.name()
                            ))
                            .unwrap();
                            config.set_redirect_url(url);
                            (*ty, config)
                        },
                    )
                })
                .collect(),
        ))
    }
}

pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
    crate::config::Web::cmd(app.about("Run a teleterm web server"))
}

pub fn config(
    config: Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>> {
    let config: Config = if let Some(config) = config {
        config
            .try_into()
            .context(crate::error::CouldntParseConfig)?
    } else {
        Config::default()
    };
    Ok(Box::new(config))
}