Move smtp relay address to the configuration
[stakingWatchdogWatchdog.git] / src / config.rs
1 use std::fs::File;
2
3 use anyhow::Result;
4 use ron::{
5 de::from_reader,
6 ser::{to_writer_pretty, PrettyConfig},
7 };
8 use serde::{Deserialize, Serialize};
9
10 #[derive(Debug, Clone, Deserialize, Serialize)]
11 pub struct Config {
12 pub staking_address: String,
13 pub smtp_relay_address: String,
14 pub smtp_login: String,
15 pub smtp_password: String,
16 }
17
18 impl Config {
19 pub fn default() -> Self {
20 Config {
21 staking_address: "192.168.2.102:8739".to_string(),
22 smtp_relay_address: "mail.something.com".to_string(),
23 smtp_login: "login".to_string(),
24 smtp_password: "password".to_string(),
25 }
26 }
27
28 pub fn read(file_path: &str) -> Result<Config> {
29 match File::open(file_path) {
30 Ok(file) => from_reader(file).map_err(|e| e.into()),
31 // The file doesn't exit -> create it with default values.
32 Err(_) => {
33 let file = File::create(file_path)?;
34 let default_config = Config::default();
35 to_writer_pretty(file, &default_config, PrettyConfig::new())?;
36 Ok(default_config)
37 }
38 }
39 }
40 }