♻️ refactor: extract builder for Relay

main
_ 2021-04-27 15:05:27 -05:00
parent cfc2228491
commit c361ecd806
4 changed files with 42 additions and 66 deletions

View File

@ -200,24 +200,24 @@ impl Builder {
Relay::try_from (self.config) Relay::try_from (self.config)
} }
pub fn enable_scraper_api (&mut self, b: bool) -> &mut Self { pub fn enable_scraper_api (mut self, b: bool) -> Self {
self.config.iso.enable_scraper_api = b; self.config.iso.enable_scraper_api = b;
self self
} }
pub fn port (&mut self, port: u16) -> &mut Self { pub fn port (mut self, port: u16) -> Self {
self.config.port = Some (port); self.config.port = Some (port);
self self
} }
pub fn scraper_key (&mut self, key: crate::key_validity::ScraperKey <crate::key_validity::Valid30Days>) pub fn scraper_key (mut self, key: crate::key_validity::ScraperKey <crate::key_validity::Valid30Days>)
-> &mut Self -> Self
{ {
self.config.scraper_keys.insert (key.hash.encode_base64 (), key); self.config.scraper_keys.insert (key.hash.encode_base64 (), key);
self self
} }
pub fn server (&mut self, server: crate::config::file::Server) -> &mut Self { pub fn server (mut self, server: crate::config::file::Server) -> Self {
self.config.servers.insert (server.name.clone (), server); self.config.servers.insert (server.name.clone (), server);
self self
} }

View File

@ -209,7 +209,7 @@ pub async fn handle (
#[cfg (test)] #[cfg (test)]
mod tests { mod tests {
use std::{ use std::{
convert::{TryFrom, TryInto}, convert::{TryInto},
}; };
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
@ -285,22 +285,20 @@ mod tests {
} }
let input = input.body (Body::empty ()).unwrap (); let input = input.body (Body::empty ()).unwrap ();
let config_file = config::file::Config { let builder = Relay::build ()
iso: config::file::Isomorphic { .port (4000)
enable_scraper_api: true, .enable_scraper_api (true);
dev_mode: Default::default (),
}, let builder = if let Some (key) = self.valid_key.map (|x| key_validity::ScraperKey::new_30_day ("automated test", x.as_bytes ())) {
port: Some (4000), builder.scraper_key (key)
servers: Some (vec! []), }
scraper_keys: Some (vec! [ else {
self.valid_key.map (|x| key_validity::ScraperKey::new_30_day ("automated test", x.as_bytes ())), builder
].into_iter ().filter_map (|x| x).collect ()),
news_url: None,
}; };
let config = config::Config::try_from (config_file).expect ("Can't load config"); let relay_state = builder.build ().expect ("Can't create relay state");
let relay_state = Arc::new (Relay::try_from (config).expect ("Can't create relay state")); let relay_state = Arc::new (relay_state);
let actual = super::handle (input, relay_state, self.path_rest).await; let actual = super::handle (input, relay_state, self.path_rest).await;
let actual = actual.expect ("Relay didn't respond"); let actual = actual.expect ("Relay didn't respond");

View File

@ -1,5 +1,4 @@
use std::{ use std::{
convert::TryFrom,
net::SocketAddr, net::SocketAddr,
path::PathBuf, path::PathBuf,
sync::Arc, sync::Arc,
@ -31,23 +30,17 @@ async fn main () -> anyhow::Result <()> {
let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate"; let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate";
let tripcode = BlakeHashWrapper::from_key (api_key.as_bytes ()); let tripcode = BlakeHashWrapper::from_key (api_key.as_bytes ());
debug! ("Relay is expecting tripcode {}", tripcode.encode_base64 ()); debug! ("Relay is expecting tripcode {}", tripcode.encode_base64 ());
let config_file = ptth_relay::config::file::Config {
iso: Default::default (),
port: Some (relay_port),
servers: Some (vec! [
ptth_relay::config::file::Server {
name: server_name.to_string (),
tripcode,
display_name: None,
},
]),
scraper_keys: None,
news_url: None,
};
let config = ptth_relay::config::Config::try_from (config_file).expect ("Can't load config"); let relay_state = ptth_relay::Relay::build ()
.port (relay_port)
.server (ptth_relay::config::file::Server {
name: server_name.to_string (),
tripcode,
display_name: None,
})
.build ().expect ("Can't create relay state");
let relay_state = Arc::new (ptth_relay::Relay::try_from (config).expect ("Can't create relay state")); let relay_state = Arc::new (relay_state);
let (stop_relay_tx, stop_relay_rx) = oneshot::channel (); let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
let task_relay = spawn ({ let task_relay = spawn ({

View File

@ -1,5 +1,5 @@
use std::{ use std::{
convert::{TryFrom, TryInto}, convert::{TryInto},
net::SocketAddr, net::SocketAddr,
path::PathBuf, path::PathBuf,
sync::{ sync::{
@ -99,23 +99,17 @@ impl TestingRelay {
let tripcode = key_validity::BlakeHashWrapper::from_key (testing_config.api_key.as_bytes ()); let tripcode = key_validity::BlakeHashWrapper::from_key (testing_config.api_key.as_bytes ());
debug! ("Relay is expecting tripcode {}", tripcode.encode_base64 ()); debug! ("Relay is expecting tripcode {}", tripcode.encode_base64 ());
let config_file = config::file::Config {
iso: Default::default (),
port: Some (testing_config.relay_port),
servers: Some (vec! [
config::file::Server {
name: testing_config.server_name.to_string (),
tripcode,
display_name: None,
},
]),
scraper_keys: None,
news_url: None,
};
let cfg = config::Config::try_from (config_file).expect ("Can't load config"); let state = Relay::build ()
.port (testing_config.relay_port)
.server (config::file::Server {
name: testing_config.server_name.to_string (),
tripcode,
display_name: None,
})
.build ().expect ("Can't create relay state");
let state = Arc::new (Relay::try_from (cfg).expect ("Can't create relay state")); let state = Arc::new (state);
let (stop_tx, stop_rx) = oneshot::channel (); let (stop_tx, stop_rx) = oneshot::channel ();
let task = spawn ({ let task = spawn ({
@ -273,22 +267,13 @@ async fn debug_proxy () {
async fn scraper_endpoints () { async fn scraper_endpoints () {
use ptth_relay::*; use ptth_relay::*;
let config_file = config::file::Config { let relay_state = Relay::build ()
iso: config::file::Isomorphic { .port (4001)
enable_scraper_api: true, .enable_scraper_api (true)
dev_mode: Default::default (), .scraper_key (key_validity::ScraperKey::new_30_day ("automated testing", b"bogus"))
}, .build ().expect ("Can't create relay state");
port: Some (4001),
servers: None,
scraper_keys: Some (vec! [
key_validity::ScraperKey::new_30_day ("automated testing", b"bogus")
]),
news_url: None,
};
let config = config::Config::try_from (config_file).expect ("Can't load config"); let relay_state = Arc::new (relay_state);
let relay_state = Arc::new (Relay::try_from (config).expect ("Can't create relay state"));
let relay_state_2 = relay_state.clone (); let relay_state_2 = relay_state.clone ();
let (stop_relay_tx, stop_relay_rx) = oneshot::channel (); let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
let task_relay = spawn (async move { let task_relay = spawn (async move {