Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add server function configs #412

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions examples/project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ opt-level = 'z'
[features]
default = ["ssr"]
hydrate = [
"leptos/hydrate",
"leptos_meta/hydrate",
"leptos_router/hydrate",
"dep:wasm-bindgen",
"dep:console_log",
"dep:console_error_panic_hook",
"leptos/hydrate",
"leptos_meta/hydrate",
"leptos_router/hydrate",
"dep:wasm-bindgen",
"dep:console_log",
"dep:console_error_panic_hook",
]
ssr = [
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
"dep:leptos_actix",
"dep:reqwest",
"dep:actix-web",
"dep:actix-files",
"dep:futures",
"dep:simple_logger",
"dep:serde_json",
"dep:dotenvy",
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
"dep:leptos_actix",
"dep:reqwest",
"dep:actix-web",
"dep:actix-files",
"dep:futures",
"dep:simple_logger",
"dep:serde_json",
"dep:dotenvy",
]


Expand Down Expand Up @@ -122,3 +122,7 @@ env = "dev"
#
# Optional: Defaults to false. Can also be set with the LEPTOS_HASH_FILES=false env var
hash-files = true

server-fn-prefix = "/custom/prefix"
disable-server-fn-hash = true
server-fn-mod-path = true
3 changes: 3 additions & 0 deletions examples/workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ lib-package = "front-package"
assets-dir = "project1/assets"
style-file = "project1/css/main.scss"
site-root = "target/site/project1"
server-fn-prefix = "/custom/prefix"
disable-server-fn-hash = true
server-fn-mod-path = true
15 changes: 12 additions & 3 deletions src/compile/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ fn test_project_dev() {
LEPTOS_JS_MINIFY=false \
LEPTOS_HASH_FILES=true \
LEPTOS_HASH_FILE_NAME=hash.txt \
LEPTOS_WATCH=true";
LEPTOS_WATCH=true \
SERVER_FN_PREFIX=/custom/prefix \
DISABLE_SERVER_FN_HASH=true \
SERVER_FN_MOD_PATH=true";
assert_eq!(ENV_REF, envs);

assert_snapshot!(cargo, @"cargo build --package=example --bin=example --no-default-features --features=ssr");
Expand Down Expand Up @@ -107,7 +110,10 @@ fn test_workspace_project1() {
LEPTOS_BIN_DIR=project1\\server \
LEPTOS_JS_MINIFY=false \
LEPTOS_HASH_FILES=false \
LEPTOS_WATCH=true"
LEPTOS_WATCH=true \
SERVER_FN_PREFIX=/custom/prefix \
DISABLE_SERVER_FN_HASH=true \
SERVER_FN_MOD_PATH=true"
} else {
"\
LEPTOS_OUTPUT_NAME=project1 \
Expand All @@ -119,7 +125,10 @@ fn test_workspace_project1() {
LEPTOS_BIN_DIR=project1/server \
LEPTOS_JS_MINIFY=false \
LEPTOS_HASH_FILES=false \
LEPTOS_WATCH=true"
LEPTOS_WATCH=true \
SERVER_FN_PREFIX=/custom/prefix \
DISABLE_SERVER_FN_HASH=true \
SERVER_FN_MOD_PATH=true"
};

let cli = dev_opts();
Expand Down
2 changes: 2 additions & 0 deletions src/config/dotenvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ fn overlay(conf: &mut ProjectConfig, envs: impl Iterator<Item = (String, String)
"LEPTOS_BIN_TARGET_DIR" => conf.bin_target_dir = Some(val),
"LEPTOS_BIN_CARGO_COMMAND" => conf.bin_cargo_command = Some(val),
"LEPTOS_JS_MINIFY" => conf.js_minify = val.parse()?,
"SERVER_FN_PREFIX" => conf.server_fn_prefix = Some(val),
"DISABLE_SERVER_FN_HASH" => conf.disable_server_fn_hash = true,
// put these here to suppress the warning, but there's no
// good way at the moment to pull the ProjectConfig all the way to Exe
exe::ENV_VAR_LEPTOS_TAILWIND_VERSION => {}
Expand Down
49 changes: 48 additions & 1 deletion src/config/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub struct Project {
pub hash_file: HashFile,
pub hash_files: bool,
pub js_minify: bool,
pub server_fn_prefix: Option<String>,
pub disable_server_fn_hash: bool,
pub server_fn_mod_path: bool,
}

impl Debug for Project {
Expand All @@ -64,6 +67,9 @@ impl Debug for Project {
.field("site", &self.site)
.field("end2end", &self.end2end)
.field("assets", &self.assets)
.field("server_fn_prefix", &self.server_fn_prefix)
.field("disable_server_fn_hash", &self.disable_server_fn_hash)
.field("server_fn_mod_path", &self.server_fn_mod_path)
.finish_non_exhaustive()
}
}
Expand Down Expand Up @@ -126,6 +132,9 @@ impl Project {
hash_file,
hash_files: config.hash_files,
js_minify: cli.release && cli.js_minify && config.js_minify,
server_fn_prefix: config.server_fn_prefix,
disable_server_fn_hash: config.disable_server_fn_hash,
server_fn_mod_path: config.server_fn_mod_path,
};
resolved.push(Arc::new(proj));
}
Expand Down Expand Up @@ -159,7 +168,16 @@ impl Project {
vec.push(("LEPTOS_HASH_FILE_NAME", self.hash_file.rel.to_string()));
}
if self.watch {
vec.push(("LEPTOS_WATCH", "true".to_string()))
vec.push(("LEPTOS_WATCH", true.to_string()))
}
if let Some(prefix) = self.server_fn_prefix.as_ref() {
vec.push(("SERVER_FN_PREFIX", prefix.clone()));
}
if self.disable_server_fn_hash {
vec.push(("DISABLE_SERVER_FN_HASH", true.to_string()));
}
if self.server_fn_mod_path {
vec.push(("SERVER_FN_MOD_PATH", true.to_string()));
}
vec
}
Expand Down Expand Up @@ -226,6 +244,35 @@ pub struct ProjectConfig {
#[serde(default)]
pub bin_default_features: bool,

/// The default prefix to use for server functions when generating API routes. Can be
/// overridden for individual functions using `#[server(prefix = "...")]` as usual.
///
/// This is useful to override the default prefix (`/api`) for all server functions without
/// needing to manually specify via `#[server(prefix = "...")]` on every server function.
#[serde(default)]
pub server_fn_prefix: Option<String>,

/// Whether to disable appending the server functions' hashes to the end of their API names.
///
/// This is useful when an app's client side needs a stable server API. For example, shipping
/// the CSR WASM binary in a Tauri app. Tauri app releases are dependent on each platform's
/// distribution method (e.g., the Apple App Store or the Google Play Store), which typically
/// are much slower than the frequency at which a website can be updated. In addition, it's
/// common for users to not have the latest app version installed. In these cases, the CSR WASM
/// app would need to be able to continue calling the backend server function API, so the API
/// path needs to be consistent and not have a hash appended.
#[serde(default)]
pub disable_server_fn_hash: bool,

/// Include the module path of the server function in the API route. This is an alternative
/// strategy to prevent duplicate server function API routes (the default strategy is to add
/// a hash to the end of the route). Each element of the module path will be separated by a `/`.
/// For example, a server function with a fully qualified name of `parent::child::server_fn`
/// would have an API route of `/api/parent/child/server_fn` (possibly with a
/// different prefix and a hash suffix depending on the values of the other server fn configs).
#[serde(default)]
server_fn_mod_path: bool,

#[serde(skip)]
pub config_dir: Utf8PathBuf,
#[serde(skip)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -71,6 +72,11 @@ Config {
dir: "project1/assets",
},
),
server_fn_prefix: Some(
"/custom/prefix",
),
disable_server_fn_hash: true,
server_fn_mod_path: true,
..
},
Project {
Expand Down Expand Up @@ -144,6 +150,9 @@ Config {
dir: "project2/src/assets",
},
),
server_fn_prefix: None,
disable_server_fn_hash: false,
server_fn_mod_path: false,
..
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -80,6 +81,9 @@ Config {
dir: "project2/src/assets",
},
),
server_fn_prefix: None,
disable_server_fn_hash: false,
server_fn_mod_path: false,
..
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -75,6 +76,9 @@ Config {
dir: "project2/src/assets",
},
),
server_fn_prefix: None,
disable_server_fn_hash: false,
server_fn_mod_path: false,
..
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -71,6 +72,11 @@ Config {
dir: "project1/assets",
},
),
server_fn_prefix: Some(
"/custom/prefix",
),
disable_server_fn_hash: true,
server_fn_mod_path: true,
..
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/config/tests.rs
expression: conf
snapshot_kind: text
---
Config {
projects: [
Expand Down Expand Up @@ -75,6 +76,9 @@ Config {
dir: "project2/src/assets",
},
),
server_fn_prefix: None,
disable_server_fn_hash: false,
server_fn_mod_path: false,
..
},
],
Expand Down