♻️ refactor: rename file_server::State to file_server::FileServer

main
_ 2021-04-17 19:10:52 -05:00
parent ae33337156
commit fd238d8c2b
4 changed files with 99 additions and 99 deletions

View File

@ -30,12 +30,12 @@ use ptth_server::{
file_server::{ file_server::{
self, self,
metrics, metrics,
State, FileServer,
}, },
load_toml, load_toml,
}; };
async fn handle_all (req: Request <Body>, state: Arc <State>) async fn handle_all (req: Request <Body>, state: Arc <FileServer>)
-> anyhow::Result <Response <Body>> -> anyhow::Result <Response <Body>>
{ {
use std::str::FromStr; use std::str::FromStr;
@ -51,8 +51,7 @@ async fn handle_all (req: Request <Body>, state: Arc <State>)
let ptth_req = RequestParts::from_hyper (parts.method, path_and_query, parts.headers)?; let ptth_req = RequestParts::from_hyper (parts.method, path_and_query, parts.headers)?;
let ptth_resp = file_server::serve_all ( let ptth_resp = state.serve_all (
&state,
ptth_req.method, ptth_req.method,
&ptth_req.uri, &ptth_req.uri,
&ptth_req.headers &ptth_req.headers
@ -95,7 +94,7 @@ async fn main () -> anyhow::Result <()> {
file_server::metrics::Interval::monitor (interval_writer).await; file_server::metrics::Interval::monitor (interval_writer).await;
}); });
let state = Arc::new (State::new ( let state = Arc::new (FileServer::new (
config_file.file_server_root, config_file.file_server_root,
&PathBuf::new (), &PathBuf::new (),
config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()), config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()),

View File

@ -46,7 +46,7 @@ struct DirEntry {
} }
pub async fn serve_root ( pub async fn serve_root (
state: &super::State, state: &super::FileServer,
) -> Result <Response, FileServerError> ) -> Result <Response, FileServerError>
{ {
#[derive (Serialize)] #[derive (Serialize)]

View File

@ -59,7 +59,7 @@ pub struct Config {
pub file_server_root: Option <PathBuf>, pub file_server_root: Option <PathBuf>,
} }
pub struct State { pub struct FileServer {
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,
@ -67,7 +67,7 @@ pub struct State {
pub hidden_path: Option <PathBuf>, pub hidden_path: Option <PathBuf>,
} }
impl State { impl FileServer {
pub fn new ( pub fn new (
file_server_root: Option <PathBuf>, file_server_root: Option <PathBuf>,
asset_root: &Path, asset_root: &Path,
@ -332,20 +332,21 @@ async fn stream_file (
} }
} }
/// Top-level request handler for the file server module. impl FileServer {
/// /// Top-level request handler for the file server module.
/// Passes a request to the internal file server logic. ///
/// Returns an HTTP response as HTML or JSON, depending on the request. /// Passes a request to the internal file server logic.
/// Returns an HTTP response as HTML or JSON, depending on the request.
#[instrument (level = "debug", skip (state, headers))] #[instrument (level = "debug", skip (self, headers))]
pub async fn serve_all ( pub async fn serve_all (
state: &State, &self,
method: Method, method: Method,
uri: &str, uri: &str,
headers: &HashMap <String, Vec <u8>> headers: &HashMap <String, Vec <u8>>
) )
-> Result <Response, FileServerError> -> Result <Response, FileServerError>
{ {
use internal::{ use internal::{
OutputFormat, OutputFormat,
Response::*, Response::*,
@ -364,11 +365,11 @@ pub async fn serve_all (
} }
let default_root = PathBuf::from ("./"); let default_root = PathBuf::from ("./");
let root: &std::path::Path = state.config.file_server_root let root: &std::path::Path = self.config.file_server_root
.as_ref () .as_ref ()
.unwrap_or (&default_root); .unwrap_or (&default_root);
Ok (match internal::serve_all (root, method, uri, headers, state.hidden_path.as_deref ()).await? { Ok (match internal::serve_all (root, method, uri, headers, self.hidden_path.as_deref ()).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"),
@ -388,14 +389,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 (state).await?, Root => html::serve_root (self).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 (&state.handlebars, &state.metrics_startup, path.to_string_lossy (), dir.into_inner ()).await?, OutputFormat::Html => html::serve_dir (&self.handlebars, &self.metrics_startup, path.to_string_lossy (), dir.into_inner ()).await?,
}, },
ServeFile (internal::ServeFileParams { ServeFile (internal::ServeFileParams {
file, file,
@ -424,6 +425,7 @@ pub async fn serve_all (
}, },
MarkdownPreview (s) => html::serve (s), MarkdownPreview (s) => html::serve (s),
}) })
}
} }
fn load_templates ( fn load_templates (

View File

@ -46,7 +46,7 @@ pub mod load_toml;
use errors::ServerError; use errors::ServerError;
struct State { struct State {
file_server: file_server::State, file_server: file_server::FileServer,
config: Config, config: Config,
client: Client, client: Client,
} }
@ -63,8 +63,7 @@ async fn handle_one_req (
debug! ("Handling request {}", req_id); debug! ("Handling request {}", req_id);
let response = file_server::serve_all ( let response = state.file_server.serve_all (
&state.file_server,
parts.method, parts.method,
&parts.uri, &parts.uri,
&parts.headers, &parts.headers,
@ -253,7 +252,7 @@ pub async fn run_server (
}); });
let state = Arc::new (State { let state = Arc::new (State {
file_server: file_server::State::new ( file_server: file_server::FileServer::new (
config_file.file_server_root, config_file.file_server_root,
&asset_root, &asset_root,
config_file.name, config_file.name,