dioxus_cli_config/lib.rs
1//! <div align="center">
2//! <img
3//! src="https://github.com/user-attachments/assets/6c7e227e-44ff-4e53-824a-67949051149c"
4//! alt="Build web, desktop, and mobile apps with a single codebase."
5//! width="100%"
6//! class="darkmode-image"
7//! >
8//! </div>
9//!
10//! # Dioxus CLI configuration
11//!
12//! This crate exposes the various configuration options that the Dioxus CLI sets when running the
13//! application during development.
14//!
15//! Note that these functions will return a different value when running under the CLI, so make sure
16//! not to rely on them when running in a production environment.
17//!
18//! ## Constants
19//!
20//! The various constants here are the names of the environment variables that the CLI sets. We recommend
21//! using the functions in this crate to access the values of these environment variables indirectly.
22//!
23//! The CLI uses this crate and the associated constants to *set* the environment variables, but as
24//! a consumer of the CLI, you would want to read the values of these environment variables using
25//! the provided functions.
26//!
27//! ## Example Usage
28//!
29//! We recommend using the functions here to access the values of the environment variables set by the CLI.
30//! For example, you might use the [`fullstack_address_or_localhost`] function to get the address that
31//! the CLI is requesting the application to be served on.
32//!
33//! ```rust, ignore
34//! async fn launch_axum(app: axum::Router<()>) {
35//! // Read the PORT and ADDR environment variables set by the CLI
36//! let addr = dioxus_cli_config::fullstack_address_or_localhost();
37//!
38//! // Bind to the address and serve the application
39//! let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
40//! axum::serve(listener, app.into_make_service())
41//! .await
42//! .unwrap();
43//! }
44//! ```
45//!
46//! ## Stability
47//!
48//! The *values* that these functions return are *not* guaranteed to be stable between patch releases
49//! of Dioxus. At any time, we might change the values that the CLI sets or the way that they are read.
50//!
51//! We also don't guarantee the stability of the env var names themselves. If you want to rely on a
52//! particular env var, use the defined constants in your code.
53
54use std::{
55 net::{IpAddr, Ipv4Addr, SocketAddr},
56 path::PathBuf,
57};
58
59pub const CLI_ENABLED_ENV: &str = "DIOXUS_CLI_ENABLED";
60pub const SERVER_IP_ENV: &str = "IP";
61pub const SERVER_PORT_ENV: &str = "PORT";
62pub const DEVSERVER_IP_ENV: &str = "DIOXUS_DEVSERVER_IP";
63pub const DEVSERVER_PORT_ENV: &str = "DIOXUS_DEVSERVER_PORT";
64pub const ALWAYS_ON_TOP_ENV: &str = "DIOXUS_ALWAYS_ON_TOP";
65pub const ASSET_ROOT_ENV: &str = "DIOXUS_ASSET_ROOT";
66pub const APP_TITLE_ENV: &str = "DIOXUS_APP_TITLE";
67
68#[deprecated(since = "0.6.0", note = "The CLI currently does not set this.")]
69#[doc(hidden)]
70pub const OUT_DIR: &str = "DIOXUS_OUT_DIR";
71pub const SESSION_CACHE_DIR: &str = "DIOXUS_SESSION_CACHE_DIR";
72
73/// Reads an environment variable at runtime in debug mode or at compile time in
74/// release mode. When bundling in release mode, we will not be running under the
75/// environment variables that the CLI sets, so we need to read them at compile time.
76macro_rules! read_env_config {
77 ($name:expr) => {{
78 #[cfg(debug_assertions)]
79 {
80 // In debug mode, read the environment variable set by the CLI at runtime
81 std::env::var($name).ok()
82 }
83
84 #[cfg(not(debug_assertions))]
85 {
86 // In release mode, read the environment variable set by the CLI at compile time
87 // This means the value will still be available when running the application
88 // standalone.
89 // We don't always read the environment variable at compile time to avoid rebuilding
90 // this crate when the environment variable changes.
91 option_env!($name).map(ToString::to_string)
92 }
93 }};
94}
95
96/// Get the address of the devserver for use over a raw socket
97///
98/// This returns a [`SocketAddr`], meaning that you still need to connect to it using a socket with
99/// the appropriate protocol and path.
100///
101/// For reference, the devserver typically lives on `127.0.0.1:8080` and serves the devserver websocket
102/// on `127.0.0.1:8080/_dioxus`.
103pub fn devserver_raw_addr() -> Option<SocketAddr> {
104 let ip = std::env::var(DEVSERVER_IP_ENV).ok()?;
105 let port = std::env::var(DEVSERVER_PORT_ENV).ok()?;
106
107 if cfg!(target_os = "android") {
108 // Since `adb reverse` is used for Android, the 127.0.0.1 will always be
109 // the correct IP address.
110 return Some(format!("127.0.0.1:{}", port).parse().unwrap());
111 }
112
113 format!("{}:{}", ip, port).parse().ok()
114}
115
116/// Get the address of the devserver for use over a websocket
117///
118/// This is meant for internal use, though if you are building devtools around Dioxus, this would be
119/// useful to connect as a "listener" to the devserver.
120///
121/// Unlike [`devserver_raw_addr`], this returns a string that can be used directly to connect to the
122/// devserver over a websocket. IE `ws://127.0.0.1:8080/_dioxus`.
123pub fn devserver_ws_endpoint() -> Option<String> {
124 let addr = devserver_raw_addr()?;
125 Some(format!("ws://{addr}/_dioxus"))
126}
127
128/// Get the IP that the server should be bound to.
129///
130/// This is set by the CLI and is used to bind the server to a specific address.
131/// You can manually set the ip by setting the `IP` environment variable.
132///
133/// ```sh
134/// IP=0.0.0.0 ./server
135/// ```
136pub fn server_ip() -> Option<IpAddr> {
137 std::env::var(SERVER_IP_ENV)
138 .ok()
139 .and_then(|s| s.parse().ok())
140}
141
142/// Get the port that the server should listen on.
143///
144/// This is set by the CLI and is used to bind the server to a specific port.
145/// You can manually set the port by setting the `PORT` environment variable.
146///
147/// ```sh
148/// PORT=8081 ./server
149/// ```
150pub fn server_port() -> Option<u16> {
151 std::env::var(SERVER_PORT_ENV)
152 .ok()
153 .and_then(|s| s.parse().ok())
154}
155
156/// Get the full address that the server should listen on.
157///
158/// This is a convenience function that combines the `server_ip` and `server_port` functions and then
159/// falls back to `localhost:8080` if the environment variables are not set.
160///
161/// ## Example
162///
163/// ```rust, ignore
164/// async fn launch_axum(app: axum::Router<()>) {
165/// // Read the PORT and ADDR environment variables set by the CLI
166/// let addr = dioxus_cli_config::fullstack_address_or_localhost();
167///
168/// // Bind to the address and serve the application
169/// let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
170/// axum::serve(listener, app.into_make_service())
171/// .await
172/// .unwrap();
173/// }
174/// ```
175///
176/// ## Stability
177///
178/// In the future, we might change the address from 127.0.0.1 to 0.0.0.0.
179pub fn fullstack_address_or_localhost() -> SocketAddr {
180 let ip = server_ip().unwrap_or_else(|| IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
181 let port = server_port().unwrap_or(8080);
182 SocketAddr::new(ip, port)
183}
184
185/// Get the title of the application, usually set by the Dioxus.toml.
186///
187/// This is used to set the title of the desktop window if the app itself doesn't set it.
188pub fn app_title() -> Option<String> {
189 read_env_config!("DIOXUS_APP_TITLE")
190}
191
192/// Check if the application should forced to "float" on top of other windows.
193///
194/// The CLI sets this based on the `--always-on-top` flag and the settings system.
195pub fn always_on_top() -> Option<bool> {
196 std::env::var(ALWAYS_ON_TOP_ENV)
197 .ok()
198 .and_then(|s| s.parse().ok())
199}
200
201/// Check if the CLI is enabled when running the application.
202///
203/// The CLI *always* sets this value to true when running the application.
204///
205/// ## Note
206///
207/// On Android and the Web, this *might* not be reliable since there isn't always a consistent way to
208/// pass off the CLI environment variables to the application.
209pub fn is_cli_enabled() -> bool {
210 // todo: (jon) - on android and web we should fix this...
211 std::env::var(CLI_ENABLED_ENV).is_ok()
212}
213
214/// Get the path where the application will be served from.
215///
216/// This is used by the router to format the URLs. For example, an app with a base path of `dogapp` will
217/// be served at `http://localhost:8080/dogapp`.
218///
219/// All assets will be served from this base path as well, ie `http://localhost:8080/dogapp/assets/logo.png`.
220pub fn base_path() -> Option<String> {
221 // This may trigger when compiling to the server if you depend on another crate that pulls in
222 // the web feature. It might be better for the renderers to provide the current platform
223 // as a global context
224 #[cfg(all(feature = "web", target_arch = "wasm32"))]
225 {
226 return web_base_path();
227 }
228
229 read_env_config!("DIOXUS_ASSET_ROOT")
230}
231
232#[cfg(feature = "web")]
233#[wasm_bindgen::prelude::wasm_bindgen(inline_js = r#"
234 export function getMetaContents(meta_name) {
235 const selector = document.querySelector(`meta[name="${meta_name}"]`);
236 if (!selector) {
237 return null;
238 }
239 return selector.content;
240 }
241 "#)]
242extern "C" {
243 #[wasm_bindgen(js_name = getMetaContents)]
244 pub fn get_meta_contents(selector: &str) -> Option<String>;
245}
246
247/// Get the path where the application is served from in the browser.
248///
249/// This uses wasm_bindgen on the browser to extract the base path from a meta element.
250#[cfg(feature = "web")]
251pub fn web_base_path() -> Option<String> {
252 // In debug mode, we get the base path from the meta element which can be hot reloaded and changed without recompiling
253 #[cfg(debug_assertions)]
254 {
255 thread_local! {
256 static BASE_PATH: std::cell::OnceCell<Option<String>> = const { std::cell::OnceCell::new() };
257 }
258 BASE_PATH.with(|f| f.get_or_init(|| get_meta_contents(ASSET_ROOT_ENV)).clone())
259 }
260
261 // In release mode, we get the base path from the environment variable
262 #[cfg(not(debug_assertions))]
263 {
264 option_env!("DIOXUS_ASSET_ROOT").map(ToString::to_string)
265 }
266}
267
268/// Format a meta element for the base path to be used in the output HTML
269#[doc(hidden)]
270pub fn format_base_path_meta_element(base_path: &str) -> String {
271 format!(r#"<meta name="{ASSET_ROOT_ENV}" content="{base_path}">"#,)
272}
273
274/// Get the path to the output directory where the application is being built.
275///
276/// This might not return a valid path - we don't recommend relying on this.
277#[doc(hidden)]
278#[deprecated(
279 since = "0.6.0",
280 note = "The does not set the OUT_DIR environment variable."
281)]
282pub fn out_dir() -> Option<PathBuf> {
283 #[allow(deprecated)]
284 {
285 std::env::var(OUT_DIR).ok().map(PathBuf::from)
286 }
287}
288
289/// Get the directory where this app can write to for this session that's guaranteed to be stable
290/// between reloads of the same app. This is useful for emitting state like window position and size
291/// so the app can restore it when it's next opened.
292///
293/// Note that this cache dir is really only useful for platforms that can access it. Web/Android
294/// don't have access to this directory, so it's not useful for them.
295///
296/// This is designed with desktop executables in mind.
297pub fn session_cache_dir() -> Option<PathBuf> {
298 if cfg!(target_os = "android") {
299 return Some(android_session_cache_dir());
300 }
301
302 std::env::var(SESSION_CACHE_DIR).ok().map(PathBuf::from)
303}
304
305/// The session cache directory for android
306pub fn android_session_cache_dir() -> PathBuf {
307 PathBuf::from("/data/local/tmp/dx/")
308}