From fa2ebf06213fa42f4b3874e28a5375a61f45b2b4 Mon Sep 17 00:00:00 2001 From: Yaroslav Shmelev Date: Sun, 5 Jan 2025 14:08:54 +0300 Subject: [PATCH] Add ability to configure index file --- source/river/assets/test-config.kdl | 4 ++++ source/river/src/config/internal.rs | 1 + source/river/src/config/kdl/mod.rs | 12 ++++++++++++ source/river/src/config/kdl/test.rs | 3 +++ source/river/src/files.rs | 2 +- 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/source/river/assets/test-config.kdl b/source/river/assets/test-config.kdl index d6e1e2c..81f8674 100644 --- a/source/river/assets/test-config.kdl +++ b/source/river/assets/test-config.kdl @@ -129,6 +129,10 @@ services { // // All files within the root will be available base-path "." + // Redirect to an index file on URI like "/" GET, can include multiple options + // + // This is optional. + index-file "index.html" } } } diff --git a/source/river/src/config/internal.rs b/source/river/src/config/internal.rs index db96c94..65124be 100644 --- a/source/river/src/config/internal.rs +++ b/source/river/src/config/internal.rs @@ -135,6 +135,7 @@ pub struct PathControl { #[derive(Debug, Clone)] pub struct FileServerConfig { pub(crate) name: String, + pub(crate) index_file: Vec, pub(crate) listeners: Vec, pub(crate) base_path: Option, } diff --git a/source/river/src/config/kdl/mod.rs b/source/river/src/config/kdl/mod.rs index 68b8ea2..756cfaf 100644 --- a/source/river/src/config/kdl/mod.rs +++ b/source/river/src/config/kdl/mod.rs @@ -222,10 +222,22 @@ fn extract_file_server( None }; + // Index File + // + let index_file = if let Some((_ifnode, ifargs)) = map.get("index-file") { + ifargs + .iter() + .filter_map(|arg| arg.value().as_string().map(ToOwned::to_owned)) + .collect() + } else { + vec![] + }; + Ok(FileServerConfig { name: name.to_string(), listeners: list_cfgs, base_path, + index_file, }) } diff --git a/source/river/src/config/kdl/test.rs b/source/river/src/config/kdl/test.rs index 6cd2d89..296999b 100644 --- a/source/river/src/config/kdl/test.rs +++ b/source/river/src/config/kdl/test.rs @@ -168,6 +168,7 @@ fn load_test() { }, ], base_path: Some(".".into()), + index_file: vec!["index.html".to_string()], }], daemonize: false, pid_file: Some("/tmp/river.pidfile".into()), @@ -209,10 +210,12 @@ fn load_test() { name, listeners, base_path, + index_file, } = afs; assert_eq!(*name, efs.name); assert_eq!(*listeners, efs.listeners); assert_eq!(*base_path, efs.base_path); + assert_eq!(*index_file, efs.index_file); } } diff --git a/source/river/src/files.rs b/source/river/src/files.rs index 689df92..32d6e54 100644 --- a/source/river/src/files.rs +++ b/source/river/src/files.rs @@ -18,7 +18,7 @@ pub fn river_file_server( let fsconf = StaticFilesConf { root: conf.base_path, canonicalize_uri: true, - index_file: Vec::new().into(), + index_file: conf.index_file.into(), page_404: None, precompressed: Vec::new().into(), ..Default::default()