Skip to content

Commit

Permalink
[解析配置] 将配置文件解析成结构体
Browse files Browse the repository at this point in the history
  • Loading branch information
caryxiao committed Sep 21, 2021
1 parent 4e9cd47 commit 718db76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
6 changes: 3 additions & 3 deletions frp-config/frpc_full.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ admin_port = 7400
admin_user = admin
admin_pwd = admin
# Admin assets directory. By default, these assets are bundled with frpc.
# assets_dir = ./static
assets_dir = ./static

# connections will be established in advance, default value is zero
pool_count = 5
Expand Down Expand Up @@ -90,8 +90,8 @@ tls_enable = true

# heartbeat configure, it's not recommended to modify the default value
# the default value of heartbeat_interval is 10 and heartbeat_timeout is 90
# heartbeat_interval = 30
# heartbeat_timeout = 90
heartbeat_interval = 10
heartbeat_timeout = 90

# additional meta info for client
meta_var1 = 123
Expand Down
24 changes: 14 additions & 10 deletions frp-config/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,35 @@ pub struct Common {
login_fail_exit: bool,
protocol: String,
tls_enable: bool,
tls_cert_file: String,
tls_key_file: String,
tls_trusted_ca_file: String,
tls_server_name: String,
dns_server: IpAddr,
start: String,
tls_cert_file: Option<String>,
tls_key_file: Option<String>,
tls_trusted_ca_file: Option<String>,
tls_server_name: Option<String>,
dns_server: Option<IpAddr>,
start: Option<String>,
heartbeat_interval: u8,
heartbeat_timeout: u8,
metas: Vec<String>,
metas: Option<Vec<String>>,
udp_packet_size: u16,
include_conf_files: Vec<String>,
include_conf_files: Option<Vec<String>>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
common: Common,
}


impl Config {
pub fn new(cnf_file: &Path) {
pub fn new(cnf_file: &Path) -> Config {
let def_str = include_str!("../frpc_full.ini");
let mut def_cnf = config::Config::default();
def_cnf
.merge(config::File::from_str(def_str, config::FileFormat::Ini)).unwrap()
.merge(config::File::from(cnf_file)).unwrap();
dbg!(def_cnf.get::<HashMap<String, String>>("common").unwrap());

Config {
common: def_cnf.get::<Common>("common").unwrap()
}
}
}
3 changes: 1 addition & 2 deletions frpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ fn main() {
// 获取配置文件
let cnf_file = matches.opt_get::<String>("c").unwrap().unwrap();
let cnf = frp_config::client::Config::new(Path::new(&cnf_file));
dbg!(cnf_file);
dbg!(cnf)
dbg!(cnf);
}

0 comments on commit 718db76

Please sign in to comment.