sylvia_iot_corelib/
server_config.rs

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
163
164
165
//! The top level configuration `server`.

use std::env;

use clap::{builder::RangedU64ValueParser, Arg, ArgMatches, Command};
use serde::Deserialize;

/// Server configuration object.
#[derive(Default, Deserialize)]
pub struct Config {
    /// HTTP port.
    ///
    /// Default is `1080`.
    #[serde(rename = "httpPort")]
    pub http_port: Option<u16>,
    /// HTTPS port.
    ///
    /// Default is `1443`.
    #[serde(rename = "httpsPort")]
    pub https_port: Option<u16>,
    /// HTTPS CA certificate file path.
    #[serde(rename = "cacertFile")]
    pub cacert_file: Option<String>,
    /// HTTPS certificate file path. Missing this to disable HTTPS.
    #[serde(rename = "certFile")]
    pub cert_file: Option<String>,
    /// HTTPS private key file path. Missing this to disable HTTPS.
    #[serde(rename = "keyFile")]
    pub key_file: Option<String>,
    /// Static file path.
    #[serde(rename = "staticPath")]
    pub static_path: Option<String>,
}

pub const DEF_HTTP_PORT: u16 = 1080;
pub const DEF_HTTPS_PORT: u16 = 1443;

/// To register Clap arguments.
pub fn reg_args(cmd: Command) -> Command {
    cmd.arg(
        Arg::new("server.httpport")
            .long("server.httpport")
            .help("HTTP port")
            .num_args(1)
            .value_parser(RangedU64ValueParser::<u64>::new().range(1..=65535)),
    )
    .arg(
        Arg::new("server.httpsport")
            .long("server.httpsport")
            .help("HTTPS port")
            .num_args(1)
            .value_parser(RangedU64ValueParser::<u64>::new().range(1..=65535)),
    )
    .arg(
        Arg::new("server.cacertfile")
            .long("server.cacertfile")
            .help("HTTPS CA certificate file")
            .num_args(1),
    )
    .arg(
        Arg::new("server.certfile")
            .long("server.certfile")
            .help("HTTPS certificate file")
            .num_args(1),
    )
    .arg(
        Arg::new("server.keyfile")
            .long("server.keyfile")
            .help("HTTPS private key file")
            .num_args(1),
    )
    .arg(
        Arg::new("server.static")
            .long("server.static")
            .help("Static files directory path")
            .num_args(1),
    )
}

/// To read input arguments from command-line arguments and environment variables.
///
/// This function will call [`apply_default()`] to fill missing values so you do not need call it
/// again.
pub fn read_args(args: &ArgMatches) -> Config {
    apply_default(&Config {
        http_port: match args.get_one::<u64>("server.httpport") {
            None => match env::var("SERVER_HTTP_PORT") {
                Err(_) => Some(DEF_HTTP_PORT),
                Ok(v) => match v.parse::<u16>() {
                    Err(_) => Some(DEF_HTTP_PORT),
                    Ok(v) => Some(v),
                },
            },
            Some(v) => Some(*v as u16),
        },
        https_port: match args.get_one::<u64>("server.httpsport") {
            None => match env::var("SERVER_HTTPS_PORT") {
                Err(_) => Some(DEF_HTTPS_PORT),
                Ok(v) => match v.parse::<u16>() {
                    Err(_) => Some(DEF_HTTPS_PORT),
                    Ok(v) => Some(v),
                },
            },
            Some(v) => Some(*v as u16),
        },
        cacert_file: match args.get_one::<String>("server.cacertfile") {
            None => match env::var("SERVER_CACERT_FILE") {
                Err(_) => None,
                Ok(v) => Some(v),
            },
            Some(v) => Some(v.clone()),
        },
        cert_file: match args.get_one::<String>("server.certfile") {
            None => match env::var("SERVER_CERT_FILE") {
                Err(_) => None,
                Ok(v) => Some(v),
            },
            Some(v) => Some(v.clone()),
        },
        key_file: match args.get_one::<String>("server.keyfile") {
            None => match env::var("SERVER_KEY_FILE") {
                Err(_) => None,
                Ok(v) => Some(v),
            },
            Some(v) => Some(v.clone()),
        },
        static_path: match args.get_one::<String>("server.static") {
            None => match env::var("SERVER_STATIC_PATH") {
                Err(_) => None,
                Ok(v) => Some(v),
            },
            Some(v) => Some(v.clone()),
        },
    })
}

/// Fill missing configuration with default values.
pub fn apply_default(config: &Config) -> Config {
    Config {
        http_port: match config.http_port {
            None => Some(DEF_HTTP_PORT),
            Some(v) => Some(v),
        },
        https_port: match config.https_port {
            None => Some(DEF_HTTPS_PORT),
            Some(v) => Some(v),
        },
        cacert_file: match config.cacert_file.as_ref() {
            None => None,
            Some(v) => Some(v.clone()),
        },
        cert_file: match config.cert_file.as_ref() {
            None => None,
            Some(v) => Some(v.clone()),
        },
        key_file: match config.key_file.as_ref() {
            None => None,
            Some(v) => Some(v.clone()),
        },
        static_path: match config.static_path.as_ref() {
            None => None,
            Some(v) => Some(v.clone()),
        },
    }
}