♻️ refactor: use file_server::State directly more

main
_ 2020-12-20 19:55:20 +00:00
parent 4cb0911b77
commit 96106e68fc
4 changed files with 19 additions and 31 deletions

View File

@ -56,14 +56,11 @@ async fn handle_all (req: Request <Body>, state: Arc <State>)
.unwrap_or (&default_root);
let ptth_resp = file_server::serve_all (
&state.handlebars,
&state.metrics_startup,
&**state.metrics_gauge.load (),
&state,
file_server_root,
ptth_req.method,
&ptth_req.uri,
&ptth_req.headers,
state.hidden_path.as_deref ()
&ptth_req.headers
).await?;
let mut resp = Response::builder ()
@ -100,9 +97,9 @@ async fn main () -> Result <(), anyhow::Error> {
config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ())
);
let metrics_gauge = Arc::new (ArcSwap::from_pointee (None));
let metrics_gauges = Arc::new (ArcSwap::from_pointee (None));
let gauge_writer = Arc::clone (&metrics_gauge);
let gauge_writer = Arc::clone (&metrics_gauges);
tokio::spawn (async move {
let mut interval = tokio::time::interval (std::time::Duration::from_secs (2));
@ -127,7 +124,7 @@ async fn main () -> Result <(), anyhow::Error> {
},
handlebars,
metrics_startup,
metrics_gauge,
metrics_gauges,
hidden_path: Some (path),
});

View File

@ -46,9 +46,7 @@ struct DirEntry {
}
pub async fn serve_root (
handlebars: &Handlebars <'static>,
metrics_startup: &metrics::Startup,
metrics_gauges: &Option <metrics::Gauges>,
state: &super::State,
) -> Result <Response, FileServerError>
{
#[derive (Serialize)]
@ -58,11 +56,11 @@ pub async fn serve_root (
}
let params = RootHtml {
metrics_startup,
metrics_gauges,
metrics_startup: &state.metrics_startup,
metrics_gauges: &**state.metrics_gauges.load (),
};
let s = handlebars.render ("file_server_root", &params)?;
let s = state.handlebars.render ("file_server_root", &params)?;
Ok (serve_html (s))
}

View File

@ -59,7 +59,7 @@ pub struct State {
pub config: Config,
pub handlebars: handlebars::Handlebars <'static>,
pub metrics_startup: metrics::Startup,
pub metrics_gauge: Arc <ArcSwap <Option <metrics::Gauges>>>,
pub metrics_gauges: Arc <ArcSwap <Option <metrics::Gauges>>>,
pub hidden_path: Option <PathBuf>,
}
@ -215,16 +215,13 @@ async fn serve_file (
// When it returns, prettify it as HTML or JSON based on what the client
// asked for.
#[instrument (level = "debug", skip (handlebars, headers, metrics_startup))]
#[instrument (level = "debug", skip (state, headers))]
pub async fn serve_all (
handlebars: &Handlebars <'static>,
metrics_startup: &metrics::Startup,
metrics_gauges: &Option <metrics::Gauges>,
state: &State,
root: &Path,
method: Method,
uri: &str,
headers: &HashMap <String, Vec <u8>>,
hidden_path: Option <&Path>
headers: &HashMap <String, Vec <u8>>
)
-> Result <Response, FileServerError>
{
@ -245,7 +242,7 @@ pub async fn serve_all (
resp
}
Ok (match internal::serve_all (root, method, uri, headers, hidden_path).await? {
Ok (match internal::serve_all (root, method, uri, headers, state.hidden_path.as_ref ().map (|p| p.as_path ())).await? {
Favicon => serve_error (StatusCode::NotFound, "Not found\n"),
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden\n"),
MethodNotAllowed => serve_error (StatusCode::MethodNotAllowed, "Unsupported method\n"),
@ -265,14 +262,14 @@ pub async fn serve_all (
},
InvalidQuery => serve_error (StatusCode::BadRequest, "Query is invalid for this object\n"),
Root => html::serve_root (handlebars, metrics_startup, metrics_gauges).await?,
Root => html::serve_root (state).await?,
ServeDir (internal::ServeDirParams {
path,
dir,
format
}) => match format {
OutputFormat::Json => serve_dir_json (dir.into_inner ()).await?,
OutputFormat::Html => html::serve_dir (handlebars, metrics_startup, path.to_string_lossy (), dir.into_inner ()).await?,
OutputFormat::Html => html::serve_dir (&state.handlebars, &state.metrics_startup, path.to_string_lossy (), dir.into_inner ()).await?,
},
ServeFile (internal::ServeFileParams {
file,

View File

@ -19,7 +19,6 @@ use serde::Deserialize;
use tokio::{
sync::{
oneshot,
RwLock,
},
time::delay_for,
};
@ -74,14 +73,11 @@ async fn handle_one_req (
.unwrap_or (&default_root);
let response = file_server::serve_all (
&state.file_server.handlebars,
&state.file_server.metrics_startup,
&**state.file_server.metrics_gauge.load (),
&state.file_server,
file_server_root,
parts.method,
&parts.uri,
&parts.headers,
state.file_server.hidden_path.as_deref ()
).await?;
let mut resp_req = state.client
@ -205,7 +201,7 @@ pub async fn run_server (
let handlebars = file_server::load_templates (&asset_root)?;
let metrics_startup = file_server::metrics::Startup::new (config_file.name);
let metrics_gauge = Arc::new (ArcSwap::from_pointee (None));
let metrics_gauges = Arc::new (ArcSwap::from_pointee (None));
let state = Arc::new (State {
file_server: file_server::State {
@ -214,7 +210,7 @@ pub async fn run_server (
},
handlebars,
metrics_startup,
metrics_gauge,
metrics_gauges,
hidden_path,
},
config: Config {