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
|
use ini::{Ini, Properties};
use std::{
path::{Path, PathBuf},
str::FromStr,
};
use url::Url;
pub struct InitConfig {
pub db_url: String,
pub btc_data_dir: Option<PathBuf>,
}
impl InitConfig {
/// Load from a file
pub fn load_from_file(config_file: impl AsRef<Path>) -> Self {
let conf = ini::Ini::load_from_file(config_file).unwrap();
assert(section(&conf, "taler"), "CURRENCY", "BTC");
let self_conf = section(&conf, "depolymerizer-bitcoin");
Self {
db_url: require(self_conf, "DB_URL", string),
btc_data_dir: path(self_conf, "BTC_DATA_DIR"),
}
}
}
/// Taler config with depolymerizer config
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
pub base_url: Url,
pub db_url: String,
pub port: u16,
pub unix_path: Option<PathBuf>,
pub btc_data_dir: Option<PathBuf>,
pub payto: Url,
pub confirmation: u8,
pub bounce_fee: u64,
pub btc_lifetime: Option<u64>,
pub http_lifetime: Option<u64>,
}
impl Config {
/// Load from a file
pub fn load_from_file(config_file: impl AsRef<Path>) -> Self {
let conf = ini::Ini::load_from_file(config_file).unwrap();
assert(section(&conf, "taler"), "CURRENCY", "BTC");
let ex_conf = section(&conf, "exchange");
let self_conf = section(&conf, "depolymerizer-bitcoin");
Self {
base_url: require(ex_conf, "BASE_URL", url),
db_url: require(self_conf, "DB_URL", string),
port: nb(self_conf, "PORT").unwrap_or(8080),
unix_path: path(self_conf, "UNIXPATH"),
btc_data_dir: path(self_conf, "BTC_DATA_DIR"),
payto: require(self_conf, "PAYTO", url),
confirmation: nb(self_conf, "CONFIRMATION").unwrap_or(6),
bounce_fee: nb(self_conf, "BOUNCE_FEE").unwrap_or(1000),
btc_lifetime: nb(self_conf, "BTC_LIFETIME")
.and_then(|nb| (nb != 0).then(|| Some(nb)))
.unwrap_or(None),
http_lifetime: nb(self_conf, "HTTP_LIFETIME")
.and_then(|nb| (nb != 0).then(|| Some(nb)))
.unwrap_or(None),
}
}
}
/* ----- Helper functions ----- */
fn section<'a>(ini: &'a Ini, name: &str) -> &'a Properties {
ini.section(Some(name))
.unwrap_or_else(|| panic!("missing config section {}", name))
}
fn assert(properties: &Properties, name: &str, expected: &str) {
let value = require(properties, name, string);
if value != expected {
panic!("config {} expected '{}' got '{}'", name, expected, value);
}
}
fn require<T>(
properties: &Properties,
name: &str,
lambda: fn(properties: &Properties, name: &str) -> Option<T>,
) -> T {
let result = lambda(properties, name);
result.unwrap_or_else(|| panic!("missing config {}", name))
}
fn string(properties: &Properties, name: &str) -> Option<String> {
properties.get(name).map(|s| s.to_string())
}
fn path(properties: &Properties, name: &str) -> Option<PathBuf> {
properties.get(name).map(|s| {
PathBuf::from_str(s).unwrap_or_else(|_| panic!("config value {} is not a valid path", name))
})
}
fn nb<T: FromStr>(properties: &Properties, name: &str) -> Option<T> {
properties.get(name).map(|s| {
s.parse()
.unwrap_or_else(|_| panic!("config value {} is not a number", name))
})
}
fn url(properties: &Properties, name: &str) -> Option<Url> {
properties.get(name).map(|s| {
Url::parse(s).unwrap_or_else(|_| panic!("config value {} is not a valid url", name))
})
}
|