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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! General runtime settings / configuration.

use config::{Config, ConfigError, Environment, File};
use http::Uri;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr, DurationMilliSeconds, DurationSeconds};
#[cfg(feature = "ipfs")]
use std::net::Ipv4Addr;
use std::{
    env,
    net::{IpAddr, Ipv6Addr},
    path::PathBuf,
    time::Duration,
};

mod libp2p_config;
mod pubkey_config;
pub(crate) use libp2p_config::{Dht, Libp2p, Pubsub};
pub(crate) use pubkey_config::PubkeyConfig;

#[cfg(target_os = "windows")]
const HOME_VAR: &str = "USERPROFILE";
#[cfg(not(target_os = "windows"))]
const HOME_VAR: &str = "HOME";

/// Application settings.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Settings {
    #[serde(default)]
    pub(crate) node: Node,
}

impl Settings {
    /// Node settings getter.
    pub fn node(&self) -> &Node {
        &self.node
    }
}

/// Server settings.
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Node {
    /// Monitoring settings.
    #[serde(default)]
    pub(crate) monitoring: Monitoring,
    /// Network settings.
    #[serde(default)]
    pub(crate) network: Network,
    /// Database settings.
    #[serde(default)]
    pub(crate) db: Database,
    /// Garbage collection interval.
    #[serde_as(as = "DurationSeconds<u64>")]
    pub(crate) gc_interval: Duration,
    /// Shutdown timeout.
    #[serde_as(as = "DurationSeconds<u64>")]
    pub(crate) shutdown_timeout: Duration,
}

/// Database-related settings for a homestar node.
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub(crate) struct Database {
    /// Database Url provided within the configuration file.
    ///
    /// Note: This is not used if the `DATABASE_URL` environment variable
    /// is set.
    #[serde_as(as = "Option<DisplayFromStr>")]
    pub(crate) url: Option<String>,
    /// Maximum number of connections managed by the [pool].
    ///
    /// [pool]: crate::db::Pool
    pub(crate) max_pool_size: u32,
}

/// Monitoring settings.
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Monitoring {
    /// Tokio console port.
    pub console_subscriber_port: u16,
    /// Monitoring collection interval in milliseconds.
    #[cfg(feature = "monitoring")]
    #[cfg_attr(docsrs, doc(cfg(feature = "monitoring")))]
    #[serde_as(as = "DurationMilliSeconds<u64>")]
    pub process_collector_interval: Duration,
}

/// Network settings for a homestar node.
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Network {
    /// libp2p Settings.
    pub(crate) libp2p: Libp2p,
    /// Metrics Settings.
    pub(crate) metrics: Metrics,
    /// Buffer-length for event(s) / command(s) channels.
    pub(crate) events_buffer_len: usize,
    /// RPC server settings.
    pub(crate) rpc: Rpc,
    /// Pubkey setup configuration.
    pub(crate) keypair_config: PubkeyConfig,
    /// Event handler poll cache interval in milliseconds.
    #[serde_as(as = "DurationMilliSeconds<u64>")]
    pub(crate) poll_cache_interval: Duration,
    /// IPFS settings.
    #[cfg(feature = "ipfs")]
    #[cfg_attr(docsrs, doc(cfg(feature = "ipfs")))]
    pub(crate) ipfs: Ipfs,
    /// Webserver settings
    pub(crate) webserver: Webserver,
}

/// IPFS Settings
#[cfg(feature = "ipfs")]
#[cfg_attr(docsrs, doc(cfg(feature = "ipfs")))]
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub(crate) struct Ipfs {
    /// The host where Homestar expects IPFS.
    pub(crate) host: String,
    /// The port where Homestar expects IPFS.
    pub(crate) port: u16,
}

/// Metrics settings.
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub(crate) struct Metrics {
    /// Metrics port for prometheus scraping.
    pub(crate) port: u16,
}

/// RPC server settings.
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub(crate) struct Rpc {
    /// RPC-server port.
    #[serde_as(as = "DisplayFromStr")]
    pub(crate) host: IpAddr,
    /// RPC-server max-concurrent connections.
    pub(crate) max_connections: usize,
    /// RPC-server port.
    pub(crate) port: u16,
    #[serde_as(as = "DurationSeconds<u64>")]
    /// RPC-server timeout.
    pub(crate) server_timeout: Duration,
}

/// Webserver settings
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub(crate) struct Webserver {
    /// Webserver host address.
    #[serde(with = "http_serde::uri")]
    pub(crate) host: Uri,
    /// Webserver-server port.
    pub(crate) port: u16,
    /// Webserver timeout.
    #[serde_as(as = "DurationSeconds<u64>")]
    pub(crate) timeout: Duration,
    /// Message capacity for the websocket-server.
    pub(crate) websocket_capacity: usize,
    /// Websocket-server send timeout.
    #[serde_as(as = "DurationMilliSeconds<u64>")]
    pub(crate) websocket_sender_timeout: Duration,
}

impl Default for Node {
    fn default() -> Self {
        Self {
            gc_interval: Duration::from_secs(1800),
            shutdown_timeout: Duration::from_secs(20),
            monitoring: Default::default(),
            network: Default::default(),
            db: Default::default(),
        }
    }
}

impl Node {
    /// Monitoring settings getter.
    pub fn monitoring(&self) -> &Monitoring {
        &self.monitoring
    }

    /// Network settings.
    pub fn network(&self) -> &Network {
        &self.network
    }

    /// Node shutdown timeout.
    pub fn shutdown_timeout(&self) -> Duration {
        self.shutdown_timeout
    }
}

impl Default for Database {
    fn default() -> Self {
        Self {
            max_pool_size: 100,
            url: None,
        }
    }
}

#[cfg(feature = "monitoring")]
#[cfg_attr(docsrs, doc(cfg(feature = "monitoring")))]
impl Default for Monitoring {
    fn default() -> Self {
        Self {
            process_collector_interval: Duration::from_millis(5000),
            console_subscriber_port: 6669,
        }
    }
}

#[cfg(not(feature = "monitoring"))]
impl Default for Monitoring {
    fn default() -> Self {
        Self {
            console_subscriber_port: 6669,
        }
    }
}

impl Default for Network {
    fn default() -> Self {
        Self {
            libp2p: Libp2p::default(),
            metrics: Metrics::default(),
            events_buffer_len: 1024,
            rpc: Rpc::default(),
            keypair_config: PubkeyConfig::Random,
            poll_cache_interval: Duration::from_millis(1000),
            #[cfg(feature = "ipfs")]
            #[cfg_attr(docsrs, doc(cfg(feature = "ipfs")))]
            ipfs: Default::default(),
            webserver: Webserver::default(),
        }
    }
}

impl Network {
    /// IPFS settings.
    #[cfg(feature = "ipfs")]
    #[cfg_attr(docsrs, doc(cfg(feature = "ipfs")))]
    pub(crate) fn ipfs(&self) -> &Ipfs {
        &self.ipfs
    }

    /// libp2p settings.
    pub(crate) fn libp2p(&self) -> &Libp2p {
        &self.libp2p
    }

    /// Webserver settings.
    pub(crate) fn webserver(&self) -> &Webserver {
        &self.webserver
    }
}

#[cfg(feature = "ipfs")]
#[cfg_attr(docsrs, doc(cfg(feature = "ipfs")))]
impl Default for Ipfs {
    fn default() -> Self {
        Self {
            host: Ipv4Addr::LOCALHOST.to_string(),
            port: 5001,
        }
    }
}

impl Default for Metrics {
    fn default() -> Self {
        Self { port: 4000 }
    }
}

impl Default for Rpc {
    fn default() -> Self {
        Self {
            host: IpAddr::V6(Ipv6Addr::LOCALHOST),
            max_connections: 10,
            port: 3030,
            server_timeout: Duration::new(120, 0),
        }
    }
}

impl Default for Webserver {
    fn default() -> Self {
        Self {
            host: Uri::from_static("127.0.0.1"),
            port: 1337,
            timeout: Duration::new(120, 0),
            websocket_capacity: 2048,
            websocket_sender_timeout: Duration::from_millis(30_000),
        }
    }
}

impl Settings {
    /// Load settings.
    ///
    /// Inject environment variables naming them properly on the settings,
    /// e.g. HOMESTAR__NODE__DB__MAX_POOL_SIZE=10.
    ///
    /// Use two underscores as defined by the separator below
    pub fn load() -> Result<Self, ConfigError> {
        #[cfg(test)]
        {
            let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("config/settings.toml");
            Self::build(Some(path))
        }
        #[cfg(not(test))]
        Self::build(None)
    }

    /// Load settings from file string that must conform to a [PathBuf].
    pub fn load_from_file(file: PathBuf) -> Result<Self, ConfigError> {
        Self::build(Some(file))
    }

    fn build(path: Option<PathBuf>) -> Result<Self, ConfigError> {
        let builder = if let Some(p) = path {
            Config::builder().add_source(File::with_name(
                &p.canonicalize()
                    .map_err(|e| ConfigError::NotFound(e.to_string()))?
                    .as_path()
                    .display()
                    .to_string(),
            ))
        } else {
            Config::builder()
        };

        let s = builder
            .add_source(Environment::with_prefix("HOMESTAR").separator("__"))
            .build()?;
        s.try_deserialize()
    }
}

#[allow(dead_code)]
fn config_dir() -> PathBuf {
    let config_dir =
        env::var("XDG_CONFIG_HOME").map_or_else(|_| home_dir().join(".config"), PathBuf::from);
    config_dir.join("homestar")
}

#[allow(dead_code)]
fn home_dir() -> PathBuf {
    let home = env::var(HOME_VAR).unwrap_or_else(|_| panic!("{} not found", HOME_VAR));
    PathBuf::from(home)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn defaults() {
        let settings = Settings::load().unwrap();
        let node_settings = settings.node;

        let default_settings = Node {
            gc_interval: Duration::from_secs(1800),
            shutdown_timeout: Duration::from_secs(20),
            ..Default::default()
        };

        assert_eq!(node_settings, default_settings);
    }

    #[test]
    fn defaults_with_modification() {
        let settings = Settings::build(Some("fixtures/settings.toml".into())).unwrap();

        let mut default_modded_settings = Node::default();
        default_modded_settings.network.events_buffer_len = 1000;
        default_modded_settings.network.webserver.port = 9999;
        default_modded_settings.gc_interval = Duration::from_secs(1800);
        default_modded_settings.shutdown_timeout = Duration::from_secs(20);
        default_modded_settings.network.libp2p.node_addresses =
            vec!["/ip4/127.0.0.1/tcp/9998/ws".to_string().try_into().unwrap()];
        assert_eq!(settings.node(), &default_modded_settings);
    }

    #[test]
    #[serial_test::parallel]
    fn default_config() {
        let settings = Settings::load().unwrap();
        let default_config = Settings::build(Some("config/defaults.toml".into()))
            .expect("default settings file in test fixtures");
        assert_eq!(settings, default_config);
    }

    #[test]
    #[serial_test::file_serial]
    fn overriding_env_serial() {
        std::env::set_var("HOMESTAR__NODE__NETWORK__RPC__PORT", "2046");
        std::env::set_var("HOMESTAR__NODE__DB__MAX_POOL_SIZE", "1");
        let settings = Settings::build(Some("fixtures/settings.toml".into())).unwrap();
        assert_eq!(settings.node.network.rpc.port, 2046);
        assert_eq!(settings.node.db.max_pool_size, 1);
    }

    #[test]
    fn import_existing_key() {
        let settings = Settings::build(Some("fixtures/settings-import-ed25519.toml".into()))
            .expect("setting file in test fixtures");

        let msg = b"foo bar";
        let signature = libp2p::identity::Keypair::ed25519_from_bytes([0; 32])
            .unwrap()
            .sign(msg)
            .unwrap();
        // round-about way of testing since there is no Eq derive for keypairs
        assert!(settings
            .node
            .network
            .keypair_config
            .keypair()
            .expect("import ed25519 key")
            .public()
            .verify(msg, &signature));
    }

    #[test]
    fn import_secp256k1_key() {
        let settings = Settings::build(Some("fixtures/settings-import-secp256k1.toml".into()))
            .expect("setting file in test fixtures");

        settings
            .node
            .network
            .keypair_config
            .keypair()
            .expect("import secp256k1 key");
    }

    #[test]
    fn seeded_secp256k1_key() {
        let settings = Settings::build(Some("fixtures/settings-random-secp256k1.toml".into()))
            .expect("setting file in test fixtures");

        settings
            .node
            .network
            .keypair_config
            .keypair()
            .expect("generate a seeded secp256k1 key");
    }

    #[test]
    #[serial_test::file_serial]
    fn test_config_dir_xdg() {
        env::remove_var("HOME");
        env::set_var("XDG_CONFIG_HOME", "/home/user/custom_config");
        assert_eq!(
            config_dir(),
            PathBuf::from("/home/user/custom_config/homestar")
        );
        env::remove_var("XDG_CONFIG_HOME");
    }

    #[cfg(not(target_os = "windows"))]
    #[test]
    #[serial_test::file_serial]
    fn test_config_dir() {
        env::set_var("HOME", "/home/user");
        env::remove_var("XDG_CONFIG_HOME");
        assert_eq!(config_dir(), PathBuf::from("/home/user/.config/homestar"));
        env::remove_var("HOME");
    }

    #[cfg(target_os = "windows")]
    #[test]
    #[serial_test::file_serial]
    fn test_config_dir() {
        env::remove_var("XDG_CONFIG_HOME");
        assert_eq!(
            config_dir(),
            PathBuf::from(format!(r"{}\.config\homestar", env!("USERPROFILE")))
        );
    }
}