Skip to content

Commit

Permalink
Merge branch 'main' into pwa_logos
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-mahajan-yral authored Jan 30, 2025
2 parents 0a50491 + 35c719c commit 4ce0df9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ssr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tokio = { version = "1", optional = true, features = [
"time",
] }
tower = { version = "0.4", optional = true }
tower-http = { version = "0.5", features = ["fs"], optional = true }
tower-http = { version = "0.5", features = ["fs", "cors"], optional = true }
wasm-bindgen = "=0.2.93"
thiserror = "1.0"
tracing = { version = "0.1.37", optional = true }
Expand Down
14 changes: 13 additions & 1 deletion ssr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ use axum::{
response::{IntoResponse, Response},
};
use axum::{routing::get, Router};
use hot_or_not_web_leptos_ssr::fallback::file_and_error_handler;
use hot_or_not_web_leptos_ssr::{app::App, init::AppStateBuilder, state::server::AppState};
use hot_or_not_web_leptos_ssr::{
fallback::file_and_error_handler, utils::host::is_host_a_preview_link,
};
use leptos::{get_configuration, logging::log, provide_context};
use leptos_axum::handle_server_fns_with_context;
use leptos_axum::{generate_route_list, LeptosRoutes};
use tower_http::cors::{AllowOrigin, CorsLayer};

pub async fn server_fn_handler(
State(app_state): State<AppState>,
Expand Down Expand Up @@ -139,6 +142,15 @@ async fn main() {
"/api/*fn_name",
get(server_fn_handler).post(server_fn_handler),
)
.layer(
CorsLayer::new().allow_origin(AllowOrigin::predicate(|origin, _| {
if let Ok(host) = origin.to_str() {
is_host_a_preview_link(host) || host == "yral.com"
} else {
false
}
})),
)
.leptos_routes_with_handler(routes, get(leptos_routes_handler))
.fallback(file_and_error_handler)
.with_state(res.app_state);
Expand Down
9 changes: 2 additions & 7 deletions ssr/src/page/google_redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ async fn preview_google_auth_redirector() -> Result<(), ServerFnError> {

#[cfg(feature = "ssr")]
fn is_valid_redirect_uri_inner(client_redirect_uri: &str) -> Option<()> {
use regex::Regex;
use std::sync::LazyLock;
use crate::utils::host::is_host_a_preview_link;

let parsed_uri = http::Uri::try_from(client_redirect_uri).ok()?;

Expand All @@ -65,11 +64,7 @@ fn is_valid_redirect_uri_inner(client_redirect_uri: &str) -> Option<()> {
return Some(());
}

static PR_PREVIEW_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^pr-\d*-yral-dapp-hot-or-not-web-leptos-ssr\.fly\.dev$").unwrap()
});

PR_PREVIEW_PATTERN.is_match_at(host, 0).then_some(())
is_host_a_preview_link(host).then_some(())
}

#[cfg(feature = "ssr")]
Expand Down
13 changes: 13 additions & 0 deletions ssr/src/utils/host.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::LazyLock;

pub fn get_host() -> String {
#[cfg(feature = "hydrate")]
{
Expand All @@ -24,6 +26,17 @@ pub fn show_cdao_page() -> bool {
show_cdao_condition(host)
}

#[cfg(feature = "ssr")]
pub fn is_host_a_preview_link(host: &str) -> bool {
use regex::Regex;

static PR_PREVIEW_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^pr-\d*-yral-dapp-hot-or-not-web-leptos-ssr\.fly\.dev$").unwrap()
});

PR_PREVIEW_PATTERN.is_match_at(host, 0)
}

pub fn show_preview_component() -> bool {
let host = get_host();
host.contains("yral-dapp-hot-or-not-web-leptos-ssr.fly.dev")
Expand Down

0 comments on commit 4ce0df9

Please sign in to comment.