♻️ 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 ()
}
struct ServerState {
struct State <'a> {
file_server: file_server::State <'a>,
config: Config,
handlebars: Handlebars <'static>,
instance_metrics: file_server::metrics::Startup,
gauges: RwLock <file_server::metrics::Gauges>,
client: Client,
hidden_path: Option <PathBuf>,
}
// 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.
async fn handle_one_req (
state: &Arc <ServerState>,
state: &Arc <State <'static>>,
wrapped_req: http_serde::WrappedRequest
) -> Result <(), ServerError>
{
@ -73,18 +71,18 @@ async fn handle_one_req (
debug! ("Handling request {}", req_id);
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 ()
.unwrap_or (&default_root);
let response = file_server::serve_all (
&state.handlebars,
&state.instance_metrics,
&state.file_server.handlebars,
&state.file_server.instance_metrics,
file_server_root,
parts.method,
&parts.uri,
&parts.headers,
state.hidden_path.as_deref ()
state.file_server.hidden_path.as_deref ()
).await?;
let mut resp_req = state.client
@ -123,7 +121,7 @@ async fn handle_one_req (
}
async fn handle_req_resp (
state: &Arc <ServerState>,
state: &Arc <State <'static>>,
req_resp: reqwest::Response
) -> Result <(), ServerError>
{
@ -172,7 +170,6 @@ impl ConfigFile {
#[derive (Default)]
pub struct Config {
pub relay_url: String,
pub file_server: file_server::Config,
}
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 state = Arc::new (ServerState {
config: Config {
relay_url: config_file.relay_url,
file_server: file_server::Config {
let state = Arc::new (State {
file_server: file_server::State {
config: file_server::Config {
file_server_root: config_file.file_server_root,
},
handlebars,
instance_metrics,
hidden_path,
},
config: Config {
relay_url: config_file.relay_url,
},
handlebars,
instance_metrics,
gauges: RwLock::new (file_server::metrics::Gauges {
rss_mib: 7,
}),
client,
hidden_path,
});
let mut backoff_delay = 0;
@ -243,7 +242,7 @@ pub async fn run_server (
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);