From 1c5229430fd734222a4b53b76041d1faa1d161c8 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 8 Mar 2024 14:29:29 +0100 Subject: [PATCH] Never use https in SSR mode, some more bug fixes --- build.rs | 11 +++++++++-- src/frontend/api.rs | 31 ++++++++++++++++++++++++++++--- src/frontend/app.rs | 5 +---- src/frontend/markdown.rs | 17 ++--------------- src/frontend/mod.rs | 16 ---------------- tests/common.rs | 2 +- 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/build.rs b/build.rs index 7c5dc9a..b54887b 100644 --- a/build.rs +++ b/build.rs @@ -1,13 +1,20 @@ use std::{ fs::{create_dir_all, File}, io::Result, + path::Path, }; /// Create placeholders for wasm files so that `cargo check` etc work without explicitly building /// frontend. fn main() -> Result<()> { create_dir_all("assets/dist/")?; - File::create("assets/dist/ibis.js")?; - File::create("assets/dist/ibis_bg.wasm")?; + let js = "assets/dist/ibis.js"; + if !Path::new(js).exists() { + File::create(js)?; + } + let wasm = "assets/dist/ibis_bg.wasm"; + if !Path::new(wasm).exists() { + File::create(wasm)?; + } Ok(()) } diff --git a/src/frontend/api.rs b/src/frontend/api.rs index 25afbf3..1cfefa3 100644 --- a/src/frontend/api.rs +++ b/src/frontend/api.rs @@ -32,11 +32,35 @@ use url::Url; pub struct ApiClient { client: Client, pub hostname: String, + ssl: bool, } impl ApiClient { - pub fn new(client: Client, hostname: String) -> Self { - Self { client, hostname } + pub fn new(client: Client, hostname_: Option) -> Self { + let mut hostname; + let ssl; + #[cfg(not(feature = "ssr"))] + { + hostname = web_sys::window().unwrap().location().host().unwrap(); + ssl = !cfg!(debug_assertions); + } + #[cfg(feature = "ssr")] + { + hostname = crate::backend::config::IbisConfig::read() + .unwrap() + .bind + .to_string(); + ssl = false; + } + // required for tests + if let Some(hostname_) = hostname_ { + hostname = hostname_; + } + Self { + client, + hostname, + ssl, + } } async fn get_query(&self, endpoint: &str, query: Option) -> MyResult @@ -203,7 +227,8 @@ impl ApiClient { } fn request_endpoint(&self, path: &str) -> String { - format!("{}://{}{path}", http_protocol_str(), &self.hostname) + let protocol = if self.ssl { "https" } else { "http" }; + format!("{protocol}://{}{path}", &self.hostname) } } diff --git a/src/frontend/app.rs b/src/frontend/app.rs index 409cdc4..32bf941 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -2,7 +2,6 @@ use crate::{ common::LocalUserView, frontend::{ api::ApiClient, - backend_hostname, components::nav::Nav, pages::{ article::{ @@ -81,11 +80,9 @@ impl GlobalState { #[component] pub fn App() -> impl IntoView { - let backend_hostname = backend_hostname(); - provide_meta_context(); let backend_hostname = GlobalState { - api_client: ApiClient::new(Client::new(), backend_hostname.clone()), + api_client: ApiClient::new(Client::new(), None), my_profile: None, }; // Load user profile in case we are already logged in diff --git a/src/frontend/markdown.rs b/src/frontend/markdown.rs index 919c040..6689278 100644 --- a/src/frontend/markdown.rs +++ b/src/frontend/markdown.rs @@ -1,4 +1,3 @@ -use crate::frontend::backend_hostname; use markdown_it::{ parser::inline::{InlineRule, InlineState}, MarkdownIt, @@ -26,12 +25,7 @@ impl NodeValue for ArticleLink { fn render(&self, node: &Node, fmt: &mut dyn Renderer) { let mut attrs = node.attrs.clone(); - let local = backend_hostname() == self.domain; - let link = if local { - format!("/article/{}", self.title) - } else { - format!("/article/{}@{}", self.title, self.domain) - }; + let link = format!("/article/{}@{}", self.title, self.domain); attrs.push(("href", link)); fmt.open("a", &attrs); @@ -68,14 +62,7 @@ impl InlineRule for ArticleLinkScanner { } #[test] -fn test_markdown_local_article_link() { - let parser = markdown_parser(); - let rendered = parser.parse("[[Title@127.0.0.1:8081]]").render(); - assert_eq!("

Title

\n", rendered); -} - -#[test] -fn test_markdown_remote_article_link() { +fn test_markdown_article_link() { let parser = markdown_parser(); let rendered = parser.parse("[[Title@example.com]]").render(); assert_eq!( diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 3f6ec6b..79f601b 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -47,19 +47,3 @@ fn user_link(person: &DbPerson) -> impl IntoView { {user_title(person)} } } - -fn backend_hostname() -> String { - let backend_hostname; - #[cfg(not(feature = "ssr"))] - { - backend_hostname = web_sys::window().unwrap().location().host().unwrap(); - } - #[cfg(feature = "ssr")] - { - backend_hostname = crate::backend::config::IbisConfig::read() - .unwrap() - .bind - .to_string(); - } - backend_hostname -} diff --git a/tests/common.rs b/tests/common.rs index 52bdf03..c6a9b9b 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -140,7 +140,7 @@ impl IbisInstance { password: "hunter2".to_string(), }; let client = ClientBuilder::new().cookie_store(true).build().unwrap(); - let api_client = ApiClient::new(client, hostname.clone()); + let api_client = ApiClient::new(client, Some(hostname)); api_client.register(form).await.unwrap(); Self { api_client,