♻️ 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)] #[structopt (long)]
auto_gen_key: bool, auto_gen_key: bool,
#[structopt (long)]
simulate_slow_upload: bool,
#[structopt (long)] #[structopt (long)]
file_server_root: Option <PathBuf>, file_server_root: Option <PathBuf>,
@ -85,8 +88,9 @@ async fn main () -> Result <(), anyhow::Error> {
let config_file = ptth_server::ConfigFile { let config_file = ptth_server::ConfigFile {
name: opt.name.or (config_file.name).expect ("`name` must be provided in command line or config file"), name: opt.name.or (config_file.name).expect ("`name` must be provided in command line or config file"),
api_key: config_file.api_key, 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), file_server_root: opt.file_server_root.or (config_file.file_server_root),
simulate_slow_upload: opt.simulate_slow_upload,
}; };
if opt.print_tripcode { 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 // type to load an incomplete file, as long as the command line options
// complete it. // complete it.
#[derive (Default)] #[derive (Clone)]
pub struct ConfigFile { pub struct ConfigFile {
pub name: String, pub name: String,
pub api_key: String, pub api_key: String,
pub relay_url: String, pub relay_url: String,
pub file_server_root: Option <PathBuf>, pub file_server_root: Option <PathBuf>,
pub simulate_slow_upload: bool,
} }
impl ConfigFile { 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] #[must_use]
pub fn tripcode (&self) -> String { pub fn tripcode (&self) -> String {
base64::encode (blake3::hash (self.api_key.as_bytes ()).as_bytes ()) base64::encode (blake3::hash (self.api_key.as_bytes ()).as_bytes ())
@ -317,12 +329,11 @@ mod tests {
#[test] #[test]
fn tripcode_algo () { fn tripcode_algo () {
let config = ConfigFile { let config = ConfigFile::new (
name: "TestName".into (), "TestName".into (),
api_key: "PlaypenCausalPlatformCommodeImproveCatalyze".into (), "PlaypenCausalPlatformCommodeImproveCatalyze".into (),
relay_url: "".into (), "".into (),
file_server_root: None, );
};
assert_eq! (config.tripcode (), "A9rPwZyY89Ag4TJjMoyYA2NeGOm99Je6rq1s0rg8PfY=".to_string ()); 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 relay_url = format! ("http://127.0.0.1:{}", proxy_port);
let config_file = ptth_server::ConfigFile { let config_file = ptth_server::ConfigFile::new (
name: server_name.into (), server_name.into (),
api_key: api_key.into (), api_key.into (),
relay_url: format! ("{}/7ZSFUKGV", relay_url), format! ("{}/7ZSFUKGV", relay_url),
file_server_root: None, );
};
let (stop_server_tx, stop_server_rx) = oneshot::channel (); let (stop_server_tx, stop_server_rx) = oneshot::channel ();
let task_server = { let task_server = {

View File

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