diff --git a/README b/README index 3c15186..6c4a197 100644 --- a/README +++ b/README @@ -13,7 +13,7 @@ build it with `cargo b -r`, the resulting binary will be at `target/release/metasearch2`. the config.toml file is created in your current working directory on the first -run of metasearch2. alternatively, you can copy the default-config.toml in the +run of metasearch2. alternatively, you can copy the config-default.toml in the repo and rename it to config.toml. the default port is 28019. diff --git a/default-config.toml b/config-base.toml similarity index 87% rename from default-config.toml rename to config-base.toml index 631b3fb..0de2e7c 100644 --- a/default-config.toml +++ b/config-base.toml @@ -1,3 +1,5 @@ +# This is the config that's used as a fallback when a field is missing from the user's config.toml. + bind = "0.0.0.0:28019" engine_list_separator = false diff --git a/config-default.toml b/config-default.toml new file mode 100644 index 0000000..9e57e15 --- /dev/null +++ b/config-default.toml @@ -0,0 +1,13 @@ +# See https://github.com/mat-1/metasearch2/blob/master/config-base.toml and +# https://github.com/mat-1/metasearch2/blob/master/src/config.rs for some of +# the possible options + +bind = "0.0.0.0:28019" + +[ui] +# engine_list_separator = true +# version_info = true + +[engines] +# numbat = false +# fend = true diff --git a/src/config.rs b/src/config.rs index 145baa2..87887c7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,40 +9,52 @@ use crate::engines::Engine; #[derive(Deserialize, Debug)] pub struct Config { pub bind: SocketAddr, - #[serde(default)] - pub engine_list_separator: Option, - #[serde(default)] - pub version_info: Option, + pub ui: UiConfig, pub engines: EnginesConfig, } -impl Config { - pub fn read_or_create() -> eyre::Result { - let default_config_str = include_str!("../default-config.toml"); - let mut config: Config = toml::from_str(default_config_str)?; +#[derive(Deserialize, Debug)] +pub struct UiConfig { + #[serde(default)] + pub show_engine_list_separator: Option, + #[serde(default)] + pub show_version_info: Option, +} - let config_path = std::env::args().nth(1).unwrap_or("config.toml".into()); - let config_path = Path::new(&config_path); - if config_path.exists() { - let given_config = toml::from_str::(&fs::read_to_string(config_path)?)?; - config.update(given_config); - Ok(config) - } else { +#[derive(Deserialize, Debug)] +pub struct EnginesConfig { + #[serde(flatten)] + pub map: HashMap, +} + +impl Config { + pub fn read_or_create(config_path: &Path) -> eyre::Result { + let base_config_str = include_str!("../config-base.toml"); + let mut config: Config = toml::from_str(base_config_str)?; + + if !config_path.exists() { info!("No config found, creating one at {config_path:?}"); + let default_config_str = include_str!("../config-default.toml"); fs::write(config_path, default_config_str)?; - Ok(config) } + + let given_config = toml::from_str::(&fs::read_to_string(config_path)?)?; + config.update(given_config); + Ok(config) } // Update the current config with the given config. This is used to make it so - // the default-config.toml is always used as a fallback if the user decides to + // the config-base.toml is always used as a fallback if the user decides to // use the default for something. pub fn update(&mut self, new: Config) { self.bind = new.bind; - self.engine_list_separator = new.engine_list_separator.or(self.engine_list_separator); - assert_ne!(self.engine_list_separator, None); - self.version_info = new.version_info.or(self.version_info); - assert_ne!(self.version_info, None); + self.ui.show_engine_list_separator = new + .ui + .show_engine_list_separator + .or(self.ui.show_engine_list_separator); + assert_ne!(self.ui.show_engine_list_separator, None); + self.ui.show_version_info = new.ui.show_version_info.or(self.ui.show_version_info); + assert_ne!(self.ui.show_version_info, None); for (key, new) in new.engines.map { if let Some(existing) = self.engines.map.get_mut(&key) { existing.update(new); @@ -53,12 +65,6 @@ impl Config { } } -#[derive(Deserialize, Debug)] -pub struct EnginesConfig { - #[serde(flatten)] - pub map: HashMap, -} - static DEFAULT_ENABLED_FULL_ENGINE_CONFIG: Lazy = Lazy::new(FullEngineConfig::default); static DEFAULT_DISABLED_FULL_ENGINE_CONFIG: Lazy = diff --git a/src/main.rs b/src/main.rs index 8248ad2..85812ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::path::Path; + use config::Config; use tracing::error; @@ -11,7 +13,10 @@ pub mod web; async fn main() { tracing_subscriber::fmt::init(); - let config = match Config::read_or_create() { + let config_path = std::env::args().nth(1).unwrap_or("config.toml".into()); + let config_path = Path::new(&config_path); + + let config = match Config::read_or_create(config_path) { Ok(config) => config, Err(err) => { error!("Couldn't parse config:\n{err}"); diff --git a/src/web/index.rs b/src/web/index.rs index af35b57..46134e9 100644 --- a/src/web/index.rs +++ b/src/web/index.rs @@ -33,7 +33,7 @@ pub async fn index(State(config): State>) -> impl IntoResponse { input type="submit" value="Search"; } } - @if config.version_info.unwrap() { + @if config.ui.show_version_info.unwrap() { span."version-info" { @if COMMIT_HASH == "unknown" || COMMIT_HASH_SHORT == "unknown" { "Version " diff --git a/src/web/search.rs b/src/web/search.rs index 8883c36..ea98bf7 100644 --- a/src/web/search.rs +++ b/src/web/search.rs @@ -55,26 +55,17 @@ fn render_end_of_html() -> String { fn render_engine_list(engines: &[engines::Engine], config: &Config) -> PreEscaped { let mut html = String::new(); - let mut first_iter = true; - for engine in engines { - if config.engine_list_separator.unwrap() && !first_iter { + for (i, engine) in engines.iter().enumerate() { + if config.ui.show_engine_list_separator.unwrap() && i > 0 { html.push_str(" · "); } - first_iter = false; let raw_engine_id = &engine.id(); - let engine_id = if config.engine_list_separator.unwrap() { + let engine_id = if config.ui.show_engine_list_separator.unwrap() { raw_engine_id.replace('_', " ") } else { raw_engine_id.to_string() }; - html.push_str( - &html! { - span."engine-list-item" { - (engine_id) - } - } - .into_string(), - ) + html.push_str(&html! { span."engine-list-item" { (engine_id) } }.into_string()) } html! { div."engine-list" {