encore/services.rs
1//! Reaching the app's other services.
2//!
3//! ```ignore
4//! let users = encore::services::base_url("users");
5//! let resp = client.get(format!("{users}/users/{id}")).send().await?;
6//! ```
7//!
8//! Looking up a service's URL makes the calling service depend on it. Encore
9//! reads the dependency from the code, as it reads which resources a service
10//! uses: so the name must be a string literal, the function must be called
11//! directly rather than passed around, and the call must be in a service.
12
13/// The base URL the calling service reaches `service` at, for
14/// service-to-service calls, without a trailing slash: a call's path goes
15/// after it, `{base}/users/42`.
16///
17/// # Panics
18///
19/// If the runtime has no address for `service`: the app has no service of
20/// that name, or this process was started without the app's service
21/// discovery (as under `encore test`).
22pub fn base_url(service: &str) -> &'static str {
23 match crate::runtime::core()
24 .runtime_config()
25 .services
26 .get(service)
27 {
28 Some(url) => url.trim_end_matches('/'),
29 None => panic!("encore: no base URL is known for service {service:?}"),
30 }
31}