encore-dev 2.0.0-beta.15

Rust SDK for Encore applications
Documentation
//! Where the app's frontends are reached.
//!
//! [`url`] is a frontend's URL in the environment the code runs in, as to
//! send a user there or to build a link to one of its pages:
//!
//! ```ignore
//! use encore::frontends;
//!
//! let reset = format!("{}/reset?token={token}", frontends::url("web"));
//! ```
//!
//! Looking a frontend's URL up links the calling service to it. Encore reads
//! the link from the code, as it reads which services a service depends on:
//! so the name must be a string literal, written as the frontend declares it,
//! the function must be called directly rather than passed around, and the
//! call must be in a service.

/// The URL `frontend` is reached at in this environment, without a trailing
/// slash: a path goes after it, `{url}/reset`. It is the first of the
/// frontend's origins, as [`crate::env::app_meta`] lists them.
///
/// The name is the one the frontend is declared with, in its
/// `encore.frontend.json` or as the service hosting it: `web_app` does not
/// find `web-app`.
///
/// # Panics
///
/// If the runtime knows no URL for `frontend`: the app has no frontend of that
/// name, which Encore's parser reports, or the environment gives it no
/// origins.
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 is known for frontend {frontend:?}: the app has no frontend of \
             that name, or the environment gives it no origins"
        )
    })
}