encore-dev 2.0.0-beta.16

Rust SDK for Encore applications
Documentation
//! Resolving URLs for the application's frontends.
//!
//! Call [`url`] to get a frontend's base URL in the current environment, for
//! example to redirect users or construct links to frontend pages:
//!
//! ```ignore
//! use encore::frontends;
//!
//! let reset = format!("{}/reset?token={token}", frontends::url("web"));
//! ```
//!
//! Looking up a frontend URL records a dependency from the calling service to
//! that frontend. Because Encore parses this dependency statically, pass the
//! frontend's declared name as a string literal, call `frontends::url` directly
//! (rather than through a function pointer), and make the call inside a service.

/// Returns the base URL for `frontend` in the current environment, without a
/// trailing slash (so paths can be appended directly, e.g. `{url}/reset`).
///
/// This returns the first origin listed for the frontend in
/// [`crate::env::app_meta`]. `frontend` must match the exact name declared in
/// `encore.frontend.json` or the hosting service (for example, `web_app` does
/// not match `web-app`).
///
/// # Panics
///
/// Panics if no URL is configured for `frontend` — either because the app has
/// no frontend with that name or because the environment provides no origins
/// for it.
pub fn url(frontend: &str) -> &'static str {
    let meta = crate::runtime::core().app_meta();
    let origins = meta.frontend(frontend).map(|fe| fe.origins.as_slice());
    crate::gateways::first_origin(origins).unwrap_or_else(|| {
        panic!(
            "encore: no URL configured for frontend {frontend:?}: either the app has no \
             frontend with that name, or this environment provides no origins for it"
        )
    })
}