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";
72pub const BUILD_ID: &str = "DIOXUS_BUILD_ID";
73
74/// Reads an environment variable at runtime in debug mode or at compile time in
75/// release mode. When bundling in release mode, we will not be running under the
76/// environment variables that the CLI sets, so we need to read them at compile time.
77macro_rules! read_env_config {
78    ($name:expr) => {{
79        #[cfg(debug_assertions)]
80        {
81            // In debug mode, read the environment variable set by the CLI at runtime
82            std::env::var($name).ok()
83        }
84
85        #[cfg(not(debug_assertions))]
86        {
87            // In release mode, read the environment variable set by the CLI at compile time
88            // This means the value will still be available when running the application
89            // standalone.
90            // We don't always read the environment variable at compile time to avoid rebuilding
91            // this crate when the environment variable changes.
92            option_env!($name).map(ToString::to_string)
93        }
94    }};
95}
96
97/// Get the address of the devserver for use over a raw socket
98///
99/// This returns a [`SocketAddr`], meaning that you still need to connect to it using a socket with
100/// the appropriate protocol and path.
101///
102/// For reference, the devserver typically lives on `127.0.0.1:8080` and serves the devserver websocket
103/// on `127.0.0.1:8080/_dioxus`.
104pub fn devserver_raw_addr() -> Option<SocketAddr> {
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        let port = port.unwrap_or("8080".to_string());
111        return Some(format!("127.0.0.1:{}", port).parse().unwrap());
112    }
113
114    let port = port?;
115    let ip = std::env::var(DEVSERVER_IP_ENV).ok()?;
116
117    format!("{}:{}", ip, port).parse().ok()
118}
119
120/// Get the address of the devserver for use over a websocket
121///
122/// This is meant for internal use, though if you are building devtools around Dioxus, this would be
123/// useful to connect as a "listener" to the devserver.
124///
125/// Unlike [`devserver_raw_addr`], this returns a string that can be used directly to connect to the
126/// devserver over a websocket. IE `ws://127.0.0.1:8080/_dioxus`.
127pub fn devserver_ws_endpoint() -> Option<String> {
128    let addr = devserver_raw_addr()?;
129    Some(format!("ws://{addr}/_dioxus"))
130}
131
132/// Get the IP that the server should be bound to.
133///
134/// This is set by the CLI and is used to bind the server to a specific address.
135/// You can manually set the ip by setting the `IP` environment variable.
136///
137/// ```sh
138/// IP=0.0.0.0 ./server
139/// ```
140pub fn server_ip() -> Option<IpAddr> {
141    std::env::var(SERVER_IP_ENV)
142        .ok()
143        .and_then(|s| s.parse().ok())
144}
145
146/// Get the port that the server should listen on.
147///
148/// This is set by the CLI and is used to bind the server to a specific port.
149/// You can manually set the port by setting the `PORT` environment variable.
150///
151/// ```sh
152/// PORT=8081 ./server
153/// ```
154pub fn server_port() -> Option<u16> {
155    std::env::var(SERVER_PORT_ENV)
156        .ok()
157        .and_then(|s| s.parse().ok())
158}
159
160/// Get the full address that the server should listen on.
161///
162/// This is a convenience function that combines the `server_ip` and `server_port` functions and then
163/// falls back to `localhost:8080` if the environment variables are not set.
164///
165/// ## Example
166///
167/// ```rust, ignore
168/// async fn launch_axum(app: axum::Router<()>) {
169///     // Read the PORT and ADDR environment variables set by the CLI
170///     let addr = dioxus_cli_config::fullstack_address_or_localhost();
171///
172///     // Bind to the address and serve the application
173///     let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
174///     axum::serve(listener, app.into_make_service())
175///         .await
176///         .unwrap();
177/// }
178/// ```
179///
180/// ## Stability
181///
182/// In the future, we might change the address from 127.0.0.1 to 0.0.0.0.
183pub fn fullstack_address_or_localhost() -> SocketAddr {
184    let ip = server_ip().unwrap_or_else(|| IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
185    let port = server_port().unwrap_or(8080);
186    SocketAddr::new(ip, port)
187}
188
189/// Get the title of the application, usually set by the Dioxus.toml.
190///
191/// This is used to set the title of the desktop window if the app itself doesn't set it.
192pub fn app_title() -> Option<String> {
193    read_env_config!("DIOXUS_APP_TITLE")
194}
195
196/// Check if the application should forced to "float" on top of other windows.
197///
198/// The CLI sets this based on the `--always-on-top` flag and the settings system.
199pub fn always_on_top() -> Option<bool> {
200    std::env::var(ALWAYS_ON_TOP_ENV)
201        .ok()
202        .and_then(|s| s.parse().ok())
203}
204
205/// Check if the CLI is enabled when running the application.
206///
207/// The CLI *always* sets this value to true when running the application.
208///
209/// ## Note
210///
211/// On Android and the Web, this *might* not be reliable since there isn't always a consistent way to
212/// pass off the CLI environment variables to the application.
213pub fn is_cli_enabled() -> bool {
214    // todo: (jon) - on android and web we should fix this...
215    std::env::var(CLI_ENABLED_ENV).is_ok()
216}
217
218/// Get the path where the application will be served from.
219///
220/// This is used by the router to format the URLs. For example, an app with a base path of `dogapp` will
221/// be served at `http://localhost:8080/dogapp`.
222///
223/// All assets will be served from this base path as well, ie `http://localhost:8080/dogapp/assets/logo.png`.
224pub fn base_path() -> Option<String> {
225    // This may trigger when compiling to the server if you depend on another crate that pulls in
226    // the web feature. It might be better for the renderers to provide the current platform
227    // as a global context
228    #[cfg(all(feature = "web", target_arch = "wasm32"))]
229    {
230        return web_base_path();
231    }
232
233    read_env_config!("DIOXUS_ASSET_ROOT")
234}
235
236#[cfg(feature = "web")]
237#[wasm_bindgen::prelude::wasm_bindgen(inline_js = r#"
238        export function getMetaContents(meta_name) {
239            const selector = document.querySelector(`meta[name="${meta_name}"]`);
240            if (!selector) {
241                return null;
242            }
243            return selector.content;
244        }
245    "#)]
246extern "C" {
247    #[wasm_bindgen(js_name = getMetaContents)]
248    pub fn get_meta_contents(selector: &str) -> Option<String>;
249}
250
251/// Get the path where the application is served from in the browser.
252///
253/// This uses wasm_bindgen on the browser to extract the base path from a meta element.
254#[cfg(feature = "web")]
255pub fn web_base_path() -> Option<String> {
256    // In debug mode, we get the base path from the meta element which can be hot reloaded and changed without recompiling
257    #[cfg(debug_assertions)]
258    {
259        thread_local! {
260            static BASE_PATH: std::cell::OnceCell<Option<String>> = const { std::cell::OnceCell::new() };
261        }
262        BASE_PATH.with(|f| f.get_or_init(|| get_meta_contents(ASSET_ROOT_ENV)).clone())
263    }
264
265    // In release mode, we get the base path from the environment variable
266    #[cfg(not(debug_assertions))]
267    {
268        option_env!("DIOXUS_ASSET_ROOT").map(ToString::to_string)
269    }
270}
271
272/// Format a meta element for the base path to be used in the output HTML
273#[doc(hidden)]
274pub fn format_base_path_meta_element(base_path: &str) -> String {
275    format!(r#"<meta name="{ASSET_ROOT_ENV}" content="{base_path}">"#,)
276}
277
278/// Get the path to the output directory where the application is being built.
279///
280/// This might not return a valid path - we don't recommend relying on this.
281#[doc(hidden)]
282#[deprecated(
283    since = "0.6.0",
284    note = "The does not set the OUT_DIR environment variable."
285)]
286pub fn out_dir() -> Option<PathBuf> {
287    #[allow(deprecated)]
288    {
289        std::env::var(OUT_DIR).ok().map(PathBuf::from)
290    }
291}
292
293/// Get the directory where this app can write to for this session that's guaranteed to be stable
294/// between reloads of the same app. This is useful for emitting state like window position and size
295/// so the app can restore it when it's next opened.
296///
297/// Note that this cache dir is really only useful for platforms that can access it. Web/Android
298/// don't have access to this directory, so it's not useful for them.
299///
300/// This is designed with desktop executables in mind.
301pub fn session_cache_dir() -> Option<PathBuf> {
302    if cfg!(target_os = "android") {
303        return Some(android_session_cache_dir());
304    }
305
306    std::env::var(SESSION_CACHE_DIR).ok().map(PathBuf::from)
307}
308
309/// The session cache directory for android
310pub fn android_session_cache_dir() -> PathBuf {
311    PathBuf::from("/data/local/tmp/dx/")
312}
313
314/// The unique build id for this application, used to disambiguate between different builds of the same
315/// application.
316pub fn build_id() -> u64 {
317    #[cfg(target_arch = "wasm32")]
318    {
319        0
320    }
321
322    #[cfg(not(target_arch = "wasm32"))]
323    {
324        std::env::var(BUILD_ID)
325            .ok()
326            .and_then(|s| s.parse().ok())
327            .unwrap_or(0)
328    }
329}