diff --git a/README b/README index 6c4a197..29d6c99 100644 --- a/README +++ b/README @@ -17,3 +17,9 @@ 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. + +API + +metasearch has a JSON API that can be enabled by setting `api = true` in your config. to use it, +just set the `Accept: application/json` header. as the api works by serializing internal structs, +it's not guaranteed to be stable across metasearch versions. diff --git a/config-default.toml b/config-default.toml index 133c979..4e903ec 100644 --- a/config-default.toml +++ b/config-default.toml @@ -6,6 +6,7 @@ api = false [ui] # engine_list_separator = true # version_info = true +# stylesheet_url = "/themes/catppuccin-mocha.css" [engines] # numbat = false diff --git a/src/config.rs b/src/config.rs index c8a3341..98d0b80 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,6 +39,8 @@ impl Config { pub struct UiConfig { pub show_engine_list_separator: bool, pub show_version_info: bool, + pub stylesheet_url: Option, + pub stylesheet_str: Option, } #[derive(Deserialize, Debug, Default)] @@ -47,6 +49,10 @@ pub struct PartialUiConfig { pub show_engine_list_separator: Option, #[serde(default)] pub show_version_info: Option, + #[serde(default)] + pub stylesheet_url: Option, + #[serde(default)] + pub stylesheet_str: Option, } impl UiConfig { @@ -55,6 +61,7 @@ impl UiConfig { .show_engine_list_separator .unwrap_or(self.show_engine_list_separator); self.show_version_info = partial.show_version_info.unwrap_or(self.show_version_info); + self.stylesheet_url = partial.stylesheet_url.or(self.stylesheet_url.clone()); } } @@ -202,6 +209,8 @@ impl Default for Config { ui: UiConfig { show_engine_list_separator: false, show_version_info: false, + stylesheet_url: None, + stylesheet_str: None, }, image_search: ImageSearchConfig { enabled: false, diff --git a/src/web/assets/README b/src/web/assets/README new file mode 100644 index 0000000..41fea6e --- /dev/null +++ b/src/web/assets/README @@ -0,0 +1,2 @@ +files added in this directory aren't automatically made accessible, you have to add them as a route +in src/web/mod.rs (so the files are included in the binary) diff --git a/src/web/assets/themes/catppuccin-mocha.css b/src/web/assets/themes/catppuccin-mocha.css new file mode 100644 index 0000000..ad529cb --- /dev/null +++ b/src/web/assets/themes/catppuccin-mocha.css @@ -0,0 +1,29 @@ +:root { + /* body background */ + --bg-1: #11111b; + /* background of the content */ + --bg-2: #181825; + /* input suggestions background */ + --bg-3: #1e1e2e; + /* mostly borders */ + --bg-4: #313244; + + /* main text color */ + --fg-1: #cdd6f4; + /* search result description */ + --fg-2: #bac2de; + --fg-3: #a6adc8; + + /* focus outline */ + --accent: #b4befe; + + --link: #89b4fa; + --link-visited: #bc78f8; + + --positive: #7fd962; + + --syntax-string: #aad94c; + --syntax-special: #e6b673; + --syntax-constant: #d2a6ff; + --syntax-comment: #acb6bf8c; +} diff --git a/src/web/index.rs b/src/web/index.rs index ed41aa5..b38ea6d 100644 --- a/src/web/index.rs +++ b/src/web/index.rs @@ -22,6 +22,12 @@ pub async fn index(State(config): State>) -> impl IntoResponse { meta name="viewport" content="width=device-width, initial-scale=1.0"; title { "metasearch" } link rel="stylesheet" href="/style.css"; + @if let Some(stylesheet_url) = &config.ui.stylesheet_url { + link rel="stylesheet" href=(stylesheet_url); + } + @if let Some(stylesheet_str) = &config.ui.stylesheet_str { + link rel="stylesheet" href=(stylesheet_str); + } script src="/script.js" defer {} link rel="search" type="application/opensearchdescription+xml" title="metasearch" href="/opensearch.xml"; } diff --git a/src/web/mod.rs b/src/web/mod.rs index 11a58ba..e31a26c 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -4,9 +4,13 @@ pub mod index; pub mod opensearch; pub mod search; -use std::{net::SocketAddr, sync::Arc}; +use std::{convert::Infallible, net::SocketAddr, sync::Arc}; -use axum::{http::header, routing::get, Router}; +use axum::{ + http::header, + routing::{get, MethodRouter}, + Router, +}; use tracing::info; use crate::config::Config; @@ -14,34 +18,43 @@ use crate::config::Config; pub async fn run(config: Config) { let bind_addr = config.bind; + fn static_route( + content: &'static str, + content_type: &'static str, + ) -> MethodRouter + where + S: Clone + Send + Sync + 'static, + { + let response = ([(header::CONTENT_TYPE, content_type)], content); + get(|| async { response }) + } + let app = Router::new() .route("/", get(index::index)) .route( "/style.css", - get(|| async { - ( - [(header::CONTENT_TYPE, "text/css; charset=utf-8")], - include_str!("assets/style.css"), - ) - }), + static_route(include_str!("assets/style.css"), "text/css; charset=utf-8"), ) .route( "/script.js", - get(|| async { - ( - [(header::CONTENT_TYPE, "text/javascript; charset=utf-8")], - include_str!("assets/script.js"), - ) - }), + static_route( + include_str!("assets/script.js"), + "text/javascript; charset=utf-8", + ), ) .route( "/robots.txt", - get(|| async { - ( - [(header::CONTENT_TYPE, "text/plain; charset=utf-8")], - include_str!("assets/robots.txt"), - ) - }), + static_route( + include_str!("assets/robots.txt"), + "text/plain; charset=utf-8", + ), + ) + .route( + "/themes/catppuccin-mocha.css", + static_route( + include_str!("assets/themes/catppuccin-mocha.css"), + "text/css; charset=utf-8", + ), ) .route("/opensearch.xml", get(opensearch::route)) .route("/search", get(search::route)) diff --git a/src/web/search.rs b/src/web/search.rs index 20e8acb..350b722 100644 --- a/src/web/search.rs +++ b/src/web/search.rs @@ -32,6 +32,12 @@ fn render_beginning_of_html(search: &SearchQuery) -> String { " - metasearch" } link rel="stylesheet" href="/style.css"; + @if let Some(stylesheet_url) = &search.config.ui.stylesheet_url { + link rel="stylesheet" href=(stylesheet_url); + } + @if let Some(stylesheet_str) = &search.config.ui.stylesheet_str { + link rel="stylesheet" href=(stylesheet_str); + } script src="/script.js" defer {} link rel="search" type="application/opensearchdescription+xml" title="metasearch" href="/opensearch.xml"; }