1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-22 09:41:10 +00:00

Never use https in SSR mode, some more bug fixes

This commit is contained in:
Felix Ableitner 2024-03-08 14:29:29 +01:00
parent 7f85248799
commit 1c5229430f
6 changed files with 41 additions and 41 deletions

View file

@ -1,13 +1,20 @@
use std::{ use std::{
fs::{create_dir_all, File}, fs::{create_dir_all, File},
io::Result, io::Result,
path::Path,
}; };
/// Create placeholders for wasm files so that `cargo check` etc work without explicitly building /// Create placeholders for wasm files so that `cargo check` etc work without explicitly building
/// frontend. /// frontend.
fn main() -> Result<()> { fn main() -> Result<()> {
create_dir_all("assets/dist/")?; create_dir_all("assets/dist/")?;
File::create("assets/dist/ibis.js")?; let js = "assets/dist/ibis.js";
File::create("assets/dist/ibis_bg.wasm")?; if !Path::new(js).exists() {
File::create(js)?;
}
let wasm = "assets/dist/ibis_bg.wasm";
if !Path::new(wasm).exists() {
File::create(wasm)?;
}
Ok(()) Ok(())
} }

View file

@ -32,11 +32,35 @@ use url::Url;
pub struct ApiClient { pub struct ApiClient {
client: Client, client: Client,
pub hostname: String, pub hostname: String,
ssl: bool,
} }
impl ApiClient { impl ApiClient {
pub fn new(client: Client, hostname: String) -> Self { pub fn new(client: Client, hostname_: Option<String>) -> Self {
Self { client, hostname } 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<T, R>(&self, endpoint: &str, query: Option<R>) -> MyResult<T> async fn get_query<T, R>(&self, endpoint: &str, query: Option<R>) -> MyResult<T>
@ -203,7 +227,8 @@ impl ApiClient {
} }
fn request_endpoint(&self, path: &str) -> String { 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)
} }
} }

View file

@ -2,7 +2,6 @@ use crate::{
common::LocalUserView, common::LocalUserView,
frontend::{ frontend::{
api::ApiClient, api::ApiClient,
backend_hostname,
components::nav::Nav, components::nav::Nav,
pages::{ pages::{
article::{ article::{
@ -81,11 +80,9 @@ impl GlobalState {
#[component] #[component]
pub fn App() -> impl IntoView { pub fn App() -> impl IntoView {
let backend_hostname = backend_hostname();
provide_meta_context(); provide_meta_context();
let backend_hostname = GlobalState { let backend_hostname = GlobalState {
api_client: ApiClient::new(Client::new(), backend_hostname.clone()), api_client: ApiClient::new(Client::new(), None),
my_profile: None, my_profile: None,
}; };
// Load user profile in case we are already logged in // Load user profile in case we are already logged in

View file

@ -1,4 +1,3 @@
use crate::frontend::backend_hostname;
use markdown_it::{ use markdown_it::{
parser::inline::{InlineRule, InlineState}, parser::inline::{InlineRule, InlineState},
MarkdownIt, MarkdownIt,
@ -26,12 +25,7 @@ impl NodeValue for ArticleLink {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) { fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
let mut attrs = node.attrs.clone(); let mut attrs = node.attrs.clone();
let local = backend_hostname() == self.domain; let link = format!("/article/{}@{}", self.title, self.domain);
let link = if local {
format!("/article/{}", self.title)
} else {
format!("/article/{}@{}", self.title, self.domain)
};
attrs.push(("href", link)); attrs.push(("href", link));
fmt.open("a", &attrs); fmt.open("a", &attrs);
@ -68,14 +62,7 @@ impl InlineRule for ArticleLinkScanner {
} }
#[test] #[test]
fn test_markdown_local_article_link() { fn test_markdown_article_link() {
let parser = markdown_parser();
let rendered = parser.parse("[[Title@127.0.0.1:8081]]").render();
assert_eq!("<p><a href=\"/article/Title\">Title</a></p>\n", rendered);
}
#[test]
fn test_markdown_remote_article_link() {
let parser = markdown_parser(); let parser = markdown_parser();
let rendered = parser.parse("[[Title@example.com]]").render(); let rendered = parser.parse("[[Title@example.com]]").render();
assert_eq!( assert_eq!(

View file

@ -47,19 +47,3 @@ fn user_link(person: &DbPerson) -> impl IntoView {
<a href={creator_path}>{user_title(person)}</a> <a href={creator_path}>{user_title(person)}</a>
} }
} }
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
}

View file

@ -140,7 +140,7 @@ impl IbisInstance {
password: "hunter2".to_string(), password: "hunter2".to_string(),
}; };
let client = ClientBuilder::new().cookie_store(true).build().unwrap(); 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(); api_client.register(form).await.unwrap();
Self { Self {
api_client, api_client,