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
use std::{net::IpAddr, path::PathBuf, str::FromStr as _};
use ini::Ini;
use url::Url;
use crate::{
ckb_config::AppConfig as CkbConfig,
error::{Error, Result},
};
const NORMAL_CONFIG_FILE: &str = "/etc/ckbdev.conf";
const SECRET_CONFIG_FILE: &str = "/etc/ckbdev.secret.conf";
pub struct Config {
pub(crate) normal: NormalConfig,
pub(crate) secret: SecretConfig,
}
pub(crate) struct NormalConfig {
pub(crate) host: HostSection,
pub(crate) ckb: CkbSection,
}
pub(crate) struct SecretConfig {
pub(crate) qiniu: QiniuSection,
}
pub(crate) struct HostSection {
pub(crate) ip: IpAddr,
#[allow(dead_code)]
pub(crate) name: String,
}
pub(crate) struct CkbSection {
pub(crate) service_name: String,
pub(crate) bin_path: PathBuf,
pub(crate) root_dir: PathBuf,
pub(crate) data_dir: PathBuf,
pub(crate) rpc_url: Url,
}
pub(crate) struct QiniuSection {
pub(crate) access_key: String,
pub(crate) secret_key: String,
pub(crate) bucket: String,
pub(crate) domain: Url,
pub(crate) path_prefix: String,
}
impl Config {
pub fn load_from_files() -> Result<Self> {
let normal = NormalConfig::load_from_file(NORMAL_CONFIG_FILE)?;
let secret = SecretConfig::load_from_file(SECRET_CONFIG_FILE)?;
Ok(Self { normal, secret })
}
}
impl NormalConfig {
pub(crate) fn load_from_file(path: &str) -> Result<Self> {
let ini = Ini::load_from_file(path)
.map_err(|err| Error::Cfg(format!("failed to load \"{}\" since {}", path, err)))?;
let host = {
let prop = ini
.section(Some("host"))
.ok_or_else(|| Error::config_not_found("host"))?;
let ip = prop
.get("ip")
.ok_or_else(|| Error::config_not_found("host.ip"))
.and_then(|s| {
IpAddr::from_str(s).map_err(|err| {
Error::Cfg(format!("failed to parse [host.ip] since {}", err))
})
})?;
let name = prop
.get("name")
.ok_or_else(|| Error::config_not_found("host.name"))?
.to_owned();
HostSection { ip, name }
};
let ckb = {
let prop = ini
.section(Some("ckb"))
.ok_or_else(|| Error::config_not_found("ckb"))?;
let service_name = prop
.get("service_name")
.ok_or_else(|| Error::config_not_found("ckb.service_name"))?
.to_owned();
let bin_path = prop
.get("bin_path")
.ok_or_else(|| Error::config_not_found("ckb.bin_path"))
.map(PathBuf::from)?;
let root_dir = prop
.get("root_dir")
.ok_or_else(|| Error::config_not_found("ckb.root_dir"))
.map(PathBuf::from)?;
let ckb_cfg = CkbConfig::load_from_workdir(&root_dir)?;
let data_dir = root_dir.join(&ckb_cfg.data_dir);
let rpc_url = {
let url = format!("http://{}", ckb_cfg.rpc.listen_address);
Url::parse(&url).map_err(|err| {
Error::Cfg(format!(
"failed to parse CKB RPC URL [{}] since {}",
url, err
))
})
}?;
CkbSection {
service_name,
bin_path,
root_dir,
data_dir,
rpc_url,
}
};
Ok(Self { host, ckb })
}
}
impl SecretConfig {
pub(crate) fn load_from_file(path: &str) -> Result<Self> {
let ini = Ini::load_from_file(path)
.map_err(|err| Error::Cfg(format!("failed to load \"{}\" since {}", path, err)))?;
let qiniu = {
let prop = ini
.section(Some("qiniu"))
.ok_or_else(|| Error::config_not_found("qiniu"))?;
let access_key = prop
.get("access_key")
.ok_or_else(|| Error::config_not_found("qiniu.access_key"))?
.to_owned();
let secret_key = prop
.get("secret_key")
.ok_or_else(|| Error::config_not_found("qiniu.secret_key"))?
.to_owned();
let bucket = prop
.get("bucket")
.ok_or_else(|| Error::config_not_found("qiniu.bucket"))?
.to_owned();
let domain = prop
.get("domain")
.ok_or_else(|| Error::config_not_found("qiniu.domain"))
.and_then(|s| {
let u = Url::parse(s).map_err(|err| {
Error::Cfg(format!("failed to parse [qiniu.domain] since {}", err))
})?;
if u.scheme() != "http" && u.scheme() != "https" {
let msg = "invalid [qiniu.domain], scheme should be \"http\" or \"https\"";
let err = Error::Cfg(msg.to_owned());
Err(err)
} else {
Ok(u)
}
})?;
let path_prefix = prop
.get("path_prefix")
.ok_or_else(|| Error::config_not_found("qiniu.path_prefix"))?
.to_owned();
QiniuSection {
access_key,
secret_key,
bucket,
domain,
path_prefix,
}
};
Ok(Self { qiniu })
}
}