Make tests pass, and do small tweaks/cleanup

This commit is contained in:
Ben Wishovich 2022-12-21 11:51:29 -08:00
parent b4897f7a61
commit 0d314224c9
4 changed files with 26 additions and 28 deletions

View File

@ -177,15 +177,15 @@ pub fn handle_server_fns() -> Route {
/// # if false { // don't actually try to run a server in a doctest...
/// #[actix_web::main]
/// async fn main() -> std::io::Result<()> {
///
/// let addr = SocketAddr::from(([127,0,0,1],3000));
/// let conf = get_configuration().await.unwrap();
/// let addr = conf.leptos_options.site_address.clone();
/// HttpServer::new(move || {
/// let render_options: LeptosOptions = LeptosOptions::builder().pkg_path("/pkg/leptos_example").reload_port(3001).socket_address(addr.clone()).environment(&env::var("RUST_ENV")).build();
/// render_options.write_to_file();
/// let leptos_options = &conf.leptos_options;
///
/// App::new()
/// // {tail:.*} passes the remainder of the URL as the route
/// // the actual routing will be handled by `leptos_router`
/// .route("/{tail:.*}", leptos_actix::render_app_to_stream(render_options, |cx| view! { cx, <MyApp/> }))
/// .route("/{tail:.*}", leptos_actix::render_app_to_stream(leptos_options.to_owned(), |cx| view! { cx, <MyApp/> }))
/// })
/// .bind(&addr)?
/// .run()
@ -218,7 +218,6 @@ pub fn render_app_to_stream(
let integration = ServerIntegration { path: path.clone() };
provide_context(cx, RouterIntegrationContext::new(integration));
provide_context(cx, MetaContext::new());
println!("Setting Default options");
provide_context(cx, res_options_default.clone());
provide_context(cx, req.clone());
@ -226,14 +225,15 @@ pub fn render_app_to_stream(
}
};
// Because wasm-pack adds _bg to the end of the WASM filename, and we want to mantain compatibility with it's default options
let site_root = &options.site_root;
// Because wasm-pack adds _bg to the end of the WASM filename, and we want to mantain compatibility with it's default options
// we add _bg to the wasm files if cargo-leptos doesn't set the env var PACKAGE_NAME
// Otherwise we need to add _bg because wasm_pack always does. This is not the same as options.package_name, which is set regardless
let wasm_package_name;
if std::env::var("PACKAGE_NAME").is_ok() {
wasm_package_name = package_name
} else {
wasm_package_name = package_name.push_str("_bg");
let package_name = &options.package_name;
let mut wasm_package_name = package_name.clone();
if std::env::var("PACKAGE_NAME").is_err() {
wasm_package_name.push_str("_bg");
}
let site_ip = &options.site_address.ip().to_string();
@ -262,9 +262,9 @@ pub fn render_app_to_stream(
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="modulepreload" href="{pkg_path}]{package_name}.js">
<link rel="preload" href="{pkg_path}/{wasm_package_name}.wasm" as="fetch" type="application/wasm" crossorigin="">
<script type="module">import init, {{ hydrate }} from '{pkg_path}/{package_name}.js'; init('{pkg_path}]{wasm_package_name}.wasm').then(hydrate);</script>
<link rel="modulepreload" href="{site_root}/{package_name}.js">
<link rel="preload" href="{site_root}/{wasm_package_name}.wasm" as="fetch" type="application/wasm" crossorigin="">
<script type="module">import init, {{ hydrate }} from '{site_root}/{package_name}.js'; init('{site_root}/{wasm_package_name}.wasm').then(hydrate);</script>
{leptos_autoreload}
"#
);
@ -288,8 +288,7 @@ pub fn render_app_to_stream(
let third_chunk = stream.next().await;
let res_options = res_options.0.read().await;
println!("Reading Options");
println!("Response Options: {:#?}", res_options);
let (status, mut headers) = (res_options.status.clone(), res_options.headers.clone());
let status = status.unwrap_or_default();

View File

@ -23,4 +23,5 @@ leptos_meta = { path = "../../meta", default-features = false, version = "0.0",
leptos_router = { path = "../../router", default-features = false, version = "0.0", features = [
"ssr",
] }
leptos_config = { path = "../../leptos_config", default-features = false, version = "0.0"}
tokio = { version = "1.0", features = ["full"] }

View File

@ -225,6 +225,7 @@ pub type PinnedHtmlStream = Pin<Box<dyn Stream<Item = io::Result<Bytes>> + Send>
/// use axum::Router;
/// use std::{net::SocketAddr, env};
/// use leptos::*;
/// use leptos_config::get_configuration;
///
/// #[component]
/// fn MyApp(cx: Scope) -> Element {
@ -234,17 +235,14 @@ pub type PinnedHtmlStream = Pin<Box<dyn Stream<Item = io::Result<Bytes>> + Send>
/// # if false { // don't actually try to run a server in a doctest...
/// #[tokio::main]
/// async fn main() {
/// let addr = SocketAddr::from(([127, 0, 0, 1], 8082));
/// let render_options: LeptosOptions = LeptosOptions::builder()
/// .site_root("/pkg")
/// .package_name("leptos_example")
/// .site_address(addr)
/// .reload_port(3001)
/// .environment(&env::var("LEPTOS_ENV")).build();
///
///
/// let conf = get_configuration().await.unwrap();
/// let leptos_options = conf.leptos_options;
/// let addr = leptos_options.site_address.clone();
///
/// // build our application with a route
/// let app = Router::new()
/// .fallback(leptos_axum::render_app_to_stream(render_options, |cx| view! { cx, <MyApp/> }));
/// .fallback(leptos_axum::render_app_to_stream(leptos_options, |cx| view! { cx, <MyApp/> }));
///
/// // run our app with hyper
/// // `axum::Server` is a re-export of `hyper::Server`
@ -292,7 +290,7 @@ pub fn render_app_to_stream(
// we add _bg to the wasm files if cargo-leptos doesn't set the env var PACKAGE_NAME
// Otherwise we need to add _bg because wasm_pack always does. This is not the same as options.package_name, which is set regardless
let mut wasm_package_name = package_name.clone();
if std::env::var("LEPTOS__PKG_NAME").is_err() {
if std::env::var("PACKAGE_NAME").is_err() {
wasm_package_name.push_str("_bg");
}

View File

@ -158,7 +158,7 @@ pub async fn get_configuration() -> Result<ConfFile, LeptosConfigError> {
// Layer on the environment-specific values.
// Add in settings from environment variables (with a prefix of APP and '__' as separator)
// E.g. `APP_APPLICATION__PORT=5001 would set `Settings.application.port`
.add_source(config::Environment::with_prefix("LEPTOS").separator("__"))
.add_source(config::Environment::with_prefix("LEPTOS").separator("_"))
.build()?;
settings