♻️ refactor

main
_ 2020-12-20 18:21:02 +00:00
parent 1aff4389bd
commit 88c3500fd0
1 changed files with 18 additions and 19 deletions

View File

@ -51,20 +51,18 @@ pub fn password_is_bad (mut password: String) -> bool {
ac.find (BAD_PASSWORDS).is_some () ac.find (BAD_PASSWORDS).is_some ()
} }
struct ServerState { struct State <'a> {
file_server: file_server::State <'a>,
config: Config, config: Config,
handlebars: Handlebars <'static>,
instance_metrics: file_server::metrics::Startup,
gauges: RwLock <file_server::metrics::Gauges>, gauges: RwLock <file_server::metrics::Gauges>,
client: Client, client: Client,
hidden_path: Option <PathBuf>,
} }
// Unwrap a request from PTTH format and pass it into file_server. // Unwrap a request from PTTH format and pass it into file_server.
// When file_server responds, wrap it back up and stream it to the relay. // When file_server responds, wrap it back up and stream it to the relay.
async fn handle_one_req ( async fn handle_one_req (
state: &Arc <ServerState>, state: &Arc <State <'static>>,
wrapped_req: http_serde::WrappedRequest wrapped_req: http_serde::WrappedRequest
) -> Result <(), ServerError> ) -> Result <(), ServerError>
{ {
@ -73,18 +71,18 @@ async fn handle_one_req (
debug! ("Handling request {}", req_id); debug! ("Handling request {}", req_id);
let default_root = PathBuf::from ("./"); let default_root = PathBuf::from ("./");
let file_server_root: &std::path::Path = state.config.file_server.file_server_root let file_server_root: &std::path::Path = state.file_server.config.file_server_root
.as_ref () .as_ref ()
.unwrap_or (&default_root); .unwrap_or (&default_root);
let response = file_server::serve_all ( let response = file_server::serve_all (
&state.handlebars, &state.file_server.handlebars,
&state.instance_metrics, &state.file_server.instance_metrics,
file_server_root, file_server_root,
parts.method, parts.method,
&parts.uri, &parts.uri,
&parts.headers, &parts.headers,
state.hidden_path.as_deref () state.file_server.hidden_path.as_deref ()
).await?; ).await?;
let mut resp_req = state.client let mut resp_req = state.client
@ -123,7 +121,7 @@ async fn handle_one_req (
} }
async fn handle_req_resp ( async fn handle_req_resp (
state: &Arc <ServerState>, state: &Arc <State <'static>>,
req_resp: reqwest::Response req_resp: reqwest::Response
) -> Result <(), ServerError> ) -> Result <(), ServerError>
{ {
@ -172,7 +170,6 @@ impl ConfigFile {
#[derive (Default)] #[derive (Default)]
pub struct Config { pub struct Config {
pub relay_url: String, pub relay_url: String,
pub file_server: file_server::Config,
} }
pub async fn run_server ( pub async fn run_server (
@ -207,20 +204,22 @@ pub async fn run_server (
let instance_metrics = file_server::metrics::Startup::new (config_file.name); let instance_metrics = file_server::metrics::Startup::new (config_file.name);
let state = Arc::new (ServerState { let state = Arc::new (State {
config: Config { file_server: file_server::State {
relay_url: config_file.relay_url, config: file_server::Config {
file_server: file_server::Config {
file_server_root: config_file.file_server_root, file_server_root: config_file.file_server_root,
}, },
},
handlebars, handlebars,
instance_metrics, instance_metrics,
hidden_path,
},
config: Config {
relay_url: config_file.relay_url,
},
gauges: RwLock::new (file_server::metrics::Gauges { gauges: RwLock::new (file_server::metrics::Gauges {
rss_mib: 7, rss_mib: 7,
}), }),
client, client,
hidden_path,
}); });
let mut backoff_delay = 0; let mut backoff_delay = 0;
@ -243,7 +242,7 @@ pub async fn run_server (
debug! ("http_listen"); debug! ("http_listen");
let req_req = state.client.get (&format! ("{}/http_listen/{}", state.config.relay_url, state.instance_metrics.server_name)).send (); let req_req = state.client.get (&format! ("{}/http_listen/{}", state.config.relay_url, state.file_server.instance_metrics.server_name)).send ();
let err_backoff_delay = std::cmp::min (30_000, backoff_delay * 2 + 500); let err_backoff_delay = std::cmp::min (30_000, backoff_delay * 2 + 500);