diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index eb90eaf..9b9c628 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,6 +51,12 @@ pub struct MiniserveConfig { /// Default color scheme pub default_color_scheme: themes::ColorScheme, + /// The name of a directory index file to serve, like "index.html" + /// + /// Normally, when miniserve serves a directory, it creates a listing for that directory. + /// However, if a directory contains this file, miniserve will serve that file instead. + pub index: Option<std::path::PathBuf>, + /// Enable file upload pub file_upload: bool, @@ -129,6 +135,14 @@ fn run() -> Result<(), ContextualError> { let canon_path = miniserve_config.path.canonicalize().map_err(|e| { ContextualError::IOError("Failed to resolve path to be served".to_string(), e) })?; + + if let Some(index_path) = &miniserve_config.index { + let has_index: std::path::PathBuf = [&canon_path, index_path].iter().collect(); + if !has_index.exists() { + + println!("{warning} The provided index file could not be found.", warning=Color::RGB(255, 192, 0).paint("Notice:").bold()); + } + } let path_string = canon_path.to_string_lossy(); println!( @@ -244,6 +258,12 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { }; if path.is_file() { None + } else if let Some(index_file) = &app.state().index { + Some( + fs::StaticFiles::new(path) + .expect("Failed to setup static file handler") + .index_file(index_file.to_string_lossy()) + ) } else { let u_r = upload_route.clone(); Some( @@ -267,6 +287,7 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { }; let random_route = app.state().random_route.clone().unwrap_or_default(); + let uses_random_route = app.state().random_route.clone().is_some(); let full_route = format!("/{}", random_route); if let Some(s) = s { @@ -275,7 +296,7 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { // Allow file upload app.resource(&upload_route, move |r| { r.method(Method::POST) - .f(move |file| file_upload::upload_file(file, default_color_scheme)) + .f(move |file| file_upload::upload_file(file, default_color_scheme, uses_random_route)) }) // Handle directories .handler(&full_route, s) @@ -295,11 +316,7 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> { fn error_404(req: &HttpRequest<crate::MiniserveConfig>) -> Result<HttpResponse, io::Error> { let err_404 = ContextualError::RouteNotFoundError(req.path().to_string()); let default_color_scheme = req.state().default_color_scheme; - let return_address = match &req.state().random_route { - Some(random_route) => format!("/{}", random_route), - None => "/".to_string(), - }; - + let uses_random_route = req.state().random_route.is_some(); let query_params = listing::extract_query_parameters(req); let color_scheme = query_params.theme.unwrap_or(default_color_scheme); @@ -309,13 +326,13 @@ fn error_404(req: &HttpRequest<crate::MiniserveConfig>) -> Result<HttpResponse, renderer::render_error( &err_404.to_string(), StatusCode::NOT_FOUND, - &return_address, + "/", query_params.sort, query_params.order, color_scheme, default_color_scheme, false, - true, + !uses_random_route, ) .into_string(), )) |