♻️ refactor: use constructor for ConfigFile

main
_ 2021-03-21 02:49:44 +00:00
parent 453532e48e
commit ee1db02b08
4 changed files with 33 additions and 20 deletions

View File

@ -19,6 +19,9 @@ struct Opt {
#[structopt (long)]
auto_gen_key: bool,
#[structopt (long)]
simulate_slow_upload: bool,
#[structopt (long)]
file_server_root: Option <PathBuf>,
@ -85,8 +88,9 @@ async fn main () -> Result <(), anyhow::Error> {
let config_file = ptth_server::ConfigFile {
name: opt.name.or (config_file.name).expect ("`name` must be provided in command line or config file"),
api_key: config_file.api_key,
relay_url: opt.relay_url.or (config_file.relay_url).expect ("`relay_url` must be provided in command line of config file"),
relay_url: opt.relay_url.or (config_file.relay_url).expect ("`relay_url` must be provided in command line or config file"),
file_server_root: opt.file_server_root.or (config_file.file_server_root),
simulate_slow_upload: opt.simulate_slow_upload,
};
if opt.print_tripcode {

View File

@ -149,15 +149,27 @@ async fn handle_req_resp (
// type to load an incomplete file, as long as the command line options
// complete it.
#[derive (Default)]
#[derive (Clone)]
pub struct ConfigFile {
pub name: String,
pub api_key: String,
pub relay_url: String,
pub file_server_root: Option <PathBuf>,
pub simulate_slow_upload: bool,
}
impl ConfigFile {
#[must_use]
pub fn new (name: String, api_key: String, relay_url: String) -> Self {
Self {
name,
api_key,
relay_url,
file_server_root: None,
simulate_slow_upload: false,
}
}
#[must_use]
pub fn tripcode (&self) -> String {
base64::encode (blake3::hash (self.api_key.as_bytes ()).as_bytes ())
@ -317,12 +329,11 @@ mod tests {
#[test]
fn tripcode_algo () {
let config = ConfigFile {
name: "TestName".into (),
api_key: "PlaypenCausalPlatformCommodeImproveCatalyze".into (),
relay_url: "".into (),
file_server_root: None,
};
let config = ConfigFile::new (
"TestName".into (),
"PlaypenCausalPlatformCommodeImproveCatalyze".into (),
"".into (),
);
assert_eq! (config.tripcode (), "A9rPwZyY89Ag4TJjMoyYA2NeGOm99Je6rq1s0rg8PfY=".to_string ());
}

View File

@ -74,12 +74,11 @@ async fn main () -> anyhow::Result <()> {
let relay_url = format! ("http://127.0.0.1:{}", proxy_port);
let config_file = ptth_server::ConfigFile {
name: server_name.into (),
api_key: api_key.into (),
relay_url: format! ("{}/7ZSFUKGV", relay_url),
file_server_root: None,
};
let config_file = ptth_server::ConfigFile::new (
server_name.into (),
api_key.into (),
format! ("{}/7ZSFUKGV", relay_url),
);
let (stop_server_tx, stop_server_rx) = oneshot::channel ();
let task_server = {

View File

@ -147,12 +147,11 @@ impl TestingRelay {
impl TestingServer {
async fn new (testing_config: &TestingConfig) -> Self {
let config_file = ptth_server::ConfigFile {
name: testing_config.server_name.into (),
api_key: testing_config.api_key.into (),
relay_url: format! ("{}/7ZSFUKGV", testing_config.relay_url ()),
file_server_root: None,
};
let config_file = ptth_server::ConfigFile::new (
testing_config.server_name.into (),
testing_config.api_key.into (),
format! ("{}/7ZSFUKGV", testing_config.relay_url ()),
);
let (stop_tx, stop_rx) = oneshot::channel ();
let task = {