♻️ refactor: rename file_server::State to file_server::FileServer
parent
ae33337156
commit
fd238d8c2b
|
@ -30,12 +30,12 @@ use ptth_server::{
|
|||
file_server::{
|
||||
self,
|
||||
metrics,
|
||||
State,
|
||||
FileServer,
|
||||
},
|
||||
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>>
|
||||
{
|
||||
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_resp = file_server::serve_all (
|
||||
&state,
|
||||
let ptth_resp = state.serve_all (
|
||||
ptth_req.method,
|
||||
&ptth_req.uri,
|
||||
&ptth_req.headers
|
||||
|
@ -95,7 +94,7 @@ async fn main () -> anyhow::Result <()> {
|
|||
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,
|
||||
&PathBuf::new (),
|
||||
config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()),
|
||||
|
|
|
@ -46,7 +46,7 @@ struct DirEntry {
|
|||
}
|
||||
|
||||
pub async fn serve_root (
|
||||
state: &super::State,
|
||||
state: &super::FileServer,
|
||||
) -> Result <Response, FileServerError>
|
||||
{
|
||||
#[derive (Serialize)]
|
||||
|
|
|
@ -59,7 +59,7 @@ pub struct Config {
|
|||
pub file_server_root: Option <PathBuf>,
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub struct FileServer {
|
||||
pub config: Config,
|
||||
pub handlebars: handlebars::Handlebars <'static>,
|
||||
pub metrics_startup: metrics::Startup,
|
||||
|
@ -67,7 +67,7 @@ pub struct State {
|
|||
pub hidden_path: Option <PathBuf>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
impl FileServer {
|
||||
pub fn new (
|
||||
file_server_root: Option <PathBuf>,
|
||||
asset_root: &Path,
|
||||
|
@ -332,20 +332,21 @@ async fn stream_file (
|
|||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
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.
|
||||
|
||||
#[instrument (level = "debug", skip (state, headers))]
|
||||
pub async fn serve_all (
|
||||
state: &State,
|
||||
#[instrument (level = "debug", skip (self, headers))]
|
||||
pub async fn serve_all (
|
||||
&self,
|
||||
method: Method,
|
||||
uri: &str,
|
||||
headers: &HashMap <String, Vec <u8>>
|
||||
)
|
||||
-> Result <Response, FileServerError>
|
||||
{
|
||||
)
|
||||
-> Result <Response, FileServerError>
|
||||
{
|
||||
use internal::{
|
||||
OutputFormat,
|
||||
Response::*,
|
||||
|
@ -364,11 +365,11 @@ pub async fn serve_all (
|
|||
}
|
||||
|
||||
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 ()
|
||||
.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"),
|
||||
Forbidden => serve_error (StatusCode::Forbidden, "403 Forbidden\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"),
|
||||
|
||||
Root => html::serve_root (state).await?,
|
||||
Root => html::serve_root (self).await?,
|
||||
ServeDir (internal::ServeDirParams {
|
||||
path,
|
||||
dir,
|
||||
format
|
||||
}) => match format {
|
||||
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 {
|
||||
file,
|
||||
|
@ -424,6 +425,7 @@ pub async fn serve_all (
|
|||
},
|
||||
MarkdownPreview (s) => html::serve (s),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn load_templates (
|
||||
|
|
|
@ -46,7 +46,7 @@ pub mod load_toml;
|
|||
use errors::ServerError;
|
||||
|
||||
struct State {
|
||||
file_server: file_server::State,
|
||||
file_server: file_server::FileServer,
|
||||
config: Config,
|
||||
client: Client,
|
||||
}
|
||||
|
@ -63,8 +63,7 @@ async fn handle_one_req (
|
|||
|
||||
debug! ("Handling request {}", req_id);
|
||||
|
||||
let response = file_server::serve_all (
|
||||
&state.file_server,
|
||||
let response = state.file_server.serve_all (
|
||||
parts.method,
|
||||
&parts.uri,
|
||||
&parts.headers,
|
||||
|
@ -253,7 +252,7 @@ pub async fn run_server (
|
|||
});
|
||||
|
||||
let state = Arc::new (State {
|
||||
file_server: file_server::State::new (
|
||||
file_server: file_server::FileServer::new (
|
||||
config_file.file_server_root,
|
||||
&asset_root,
|
||||
config_file.name,
|
||||
|
|
Loading…
Reference in New Issue