♻️ refactor: use file_server::State directly more
parent
4cb0911b77
commit
96106e68fc
|
@ -56,14 +56,11 @@ async fn handle_all (req: Request <Body>, state: Arc <State>)
|
||||||
.unwrap_or (&default_root);
|
.unwrap_or (&default_root);
|
||||||
|
|
||||||
let ptth_resp = file_server::serve_all (
|
let ptth_resp = file_server::serve_all (
|
||||||
&state.handlebars,
|
&state,
|
||||||
&state.metrics_startup,
|
|
||||||
&**state.metrics_gauge.load (),
|
|
||||||
file_server_root,
|
file_server_root,
|
||||||
ptth_req.method,
|
ptth_req.method,
|
||||||
&ptth_req.uri,
|
&ptth_req.uri,
|
||||||
&ptth_req.headers,
|
&ptth_req.headers
|
||||||
state.hidden_path.as_deref ()
|
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
let mut resp = Response::builder ()
|
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 ())
|
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 {
|
tokio::spawn (async move {
|
||||||
let mut interval = tokio::time::interval (std::time::Duration::from_secs (2));
|
let mut interval = tokio::time::interval (std::time::Duration::from_secs (2));
|
||||||
|
@ -127,7 +124,7 @@ async fn main () -> Result <(), anyhow::Error> {
|
||||||
},
|
},
|
||||||
handlebars,
|
handlebars,
|
||||||
metrics_startup,
|
metrics_startup,
|
||||||
metrics_gauge,
|
metrics_gauges,
|
||||||
hidden_path: Some (path),
|
hidden_path: Some (path),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -46,9 +46,7 @@ struct DirEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn serve_root (
|
pub async fn serve_root (
|
||||||
handlebars: &Handlebars <'static>,
|
state: &super::State,
|
||||||
metrics_startup: &metrics::Startup,
|
|
||||||
metrics_gauges: &Option <metrics::Gauges>,
|
|
||||||
) -> Result <Response, FileServerError>
|
) -> Result <Response, FileServerError>
|
||||||
{
|
{
|
||||||
#[derive (Serialize)]
|
#[derive (Serialize)]
|
||||||
|
@ -58,11 +56,11 @@ pub async fn serve_root (
|
||||||
}
|
}
|
||||||
|
|
||||||
let params = RootHtml {
|
let params = RootHtml {
|
||||||
metrics_startup,
|
metrics_startup: &state.metrics_startup,
|
||||||
metrics_gauges,
|
metrics_gauges: &**state.metrics_gauges.load (),
|
||||||
};
|
};
|
||||||
|
|
||||||
let s = handlebars.render ("file_server_root", ¶ms)?;
|
let s = state.handlebars.render ("file_server_root", ¶ms)?;
|
||||||
|
|
||||||
Ok (serve_html (s))
|
Ok (serve_html (s))
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ pub struct State {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub handlebars: handlebars::Handlebars <'static>,
|
pub handlebars: handlebars::Handlebars <'static>,
|
||||||
pub metrics_startup: metrics::Startup,
|
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>,
|
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
|
// When it returns, prettify it as HTML or JSON based on what the client
|
||||||
// asked for.
|
// asked for.
|
||||||
|
|
||||||
#[instrument (level = "debug", skip (handlebars, headers, metrics_startup))]
|
#[instrument (level = "debug", skip (state, headers))]
|
||||||
pub async fn serve_all (
|
pub async fn serve_all (
|
||||||
handlebars: &Handlebars <'static>,
|
state: &State,
|
||||||
metrics_startup: &metrics::Startup,
|
|
||||||
metrics_gauges: &Option <metrics::Gauges>,
|
|
||||||
root: &Path,
|
root: &Path,
|
||||||
method: Method,
|
method: Method,
|
||||||
uri: &str,
|
uri: &str,
|
||||||
headers: &HashMap <String, Vec <u8>>,
|
headers: &HashMap <String, Vec <u8>>
|
||||||
hidden_path: Option <&Path>
|
|
||||||
)
|
)
|
||||||
-> Result <Response, FileServerError>
|
-> Result <Response, FileServerError>
|
||||||
{
|
{
|
||||||
|
@ -245,7 +242,7 @@ pub async fn serve_all (
|
||||||
resp
|
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"),
|
Favicon => serve_error (StatusCode::NotFound, "Not found\n"),
|
||||||
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden\n"),
|
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden\n"),
|
||||||
MethodNotAllowed => serve_error (StatusCode::MethodNotAllowed, "Unsupported method\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"),
|
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 {
|
ServeDir (internal::ServeDirParams {
|
||||||
path,
|
path,
|
||||||
dir,
|
dir,
|
||||||
format
|
format
|
||||||
}) => match format {
|
}) => match format {
|
||||||
OutputFormat::Json => serve_dir_json (dir.into_inner ()).await?,
|
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 {
|
ServeFile (internal::ServeFileParams {
|
||||||
file,
|
file,
|
||||||
|
|
|
@ -19,7 +19,6 @@ use serde::Deserialize;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
sync::{
|
sync::{
|
||||||
oneshot,
|
oneshot,
|
||||||
RwLock,
|
|
||||||
},
|
},
|
||||||
time::delay_for,
|
time::delay_for,
|
||||||
};
|
};
|
||||||
|
@ -74,14 +73,11 @@ async fn handle_one_req (
|
||||||
.unwrap_or (&default_root);
|
.unwrap_or (&default_root);
|
||||||
|
|
||||||
let response = file_server::serve_all (
|
let response = file_server::serve_all (
|
||||||
&state.file_server.handlebars,
|
&state.file_server,
|
||||||
&state.file_server.metrics_startup,
|
|
||||||
&**state.file_server.metrics_gauge.load (),
|
|
||||||
file_server_root,
|
file_server_root,
|
||||||
parts.method,
|
parts.method,
|
||||||
&parts.uri,
|
&parts.uri,
|
||||||
&parts.headers,
|
&parts.headers,
|
||||||
state.file_server.hidden_path.as_deref ()
|
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
let mut resp_req = state.client
|
let mut resp_req = state.client
|
||||||
|
@ -205,7 +201,7 @@ pub async fn run_server (
|
||||||
let handlebars = file_server::load_templates (&asset_root)?;
|
let handlebars = file_server::load_templates (&asset_root)?;
|
||||||
|
|
||||||
let metrics_startup = file_server::metrics::Startup::new (config_file.name);
|
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 {
|
let state = Arc::new (State {
|
||||||
file_server: file_server::State {
|
file_server: file_server::State {
|
||||||
|
@ -214,7 +210,7 @@ pub async fn run_server (
|
||||||
},
|
},
|
||||||
handlebars,
|
handlebars,
|
||||||
metrics_startup,
|
metrics_startup,
|
||||||
metrics_gauge,
|
metrics_gauges,
|
||||||
hidden_path,
|
hidden_path,
|
||||||
},
|
},
|
||||||
config: Config {
|
config: Config {
|
||||||
|
|
Loading…
Reference in New Issue