♻️ 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)
}
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
}
pub fn port (&mut self, port: u16) -> &mut Self {
pub fn port (mut self, port: u16) -> Self {
self.config.port = Some (port);
self
}
pub fn scraper_key (&mut self, key: crate::key_validity::ScraperKey <crate::key_validity::Valid30Days>)
-> &mut Self
pub fn scraper_key (mut self, key: crate::key_validity::ScraperKey <crate::key_validity::Valid30Days>)
-> Self
{
self.config.scraper_keys.insert (key.hash.encode_base64 (), key);
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
}

View File

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

View File

@ -1,5 +1,4 @@
use std::{
convert::TryFrom,
net::SocketAddr,
path::PathBuf,
sync::Arc,
@ -31,23 +30,17 @@ async fn main () -> anyhow::Result <()> {
let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate";
let tripcode = BlakeHashWrapper::from_key (api_key.as_bytes ());
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 {
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,
},
]),
scraper_keys: None,
news_url: None,
};
})
.build ().expect ("Can't create relay state");
let config = ptth_relay::config::Config::try_from (config_file).expect ("Can't load config");
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 task_relay = spawn ({

View File

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