encore-dev 2.0.0-alpha.16

Rust SDK for Encore applications
Documentation
//! Reaching the app's other services.
//!
//! ```ignore
//! let users = encore::services::base_url("users");
//! let resp = client.get(format!("{users}/users/{id}")).send().await?;
//! ```
//!
//! Looking up a service's URL makes the calling service depend on it. Encore
//! reads the dependency from the code, as it reads which resources a service
//! uses: so the name must be a string literal, the function must be called
//! directly rather than passed around, and the call must be in a service.

/// The base URL the calling service reaches `service` at, for
/// service-to-service calls, without a trailing slash: a call's path goes
/// after it, `{base}/users/42`.
///
/// # Panics
///
/// If the runtime has no address for `service`: the app has no service of
/// that name, or this process was started without the app's service
/// discovery (as under `encore test`).
pub fn base_url(service: &str) -> &'static str {
    match crate::runtime::core()
        .runtime_config()
        .services
        .get(service)
    {
        Some(url) => url.trim_end_matches('/'),
        None => panic!("encore: no base URL is known for service {service:?}"),
    }
}