encore/frontends.rs
1//! Resolving URLs for the application's frontends.
2//!
3//! Call [`url`] to get a frontend's base URL in the current environment, for
4//! example to redirect users or construct links to frontend pages:
5//!
6//! ```ignore
7//! use encore::frontends;
8//!
9//! let reset = format!("{}/reset?token={token}", frontends::url("web"));
10//! ```
11//!
12//! Looking up a frontend URL records a dependency from the calling service to
13//! that frontend. Because Encore parses this dependency statically, pass the
14//! frontend's declared name as a string literal, call `frontends::url` directly
15//! (rather than through a function pointer), and make the call inside a service.
16
17/// Returns the base URL for `frontend` in the current environment, without a
18/// trailing slash (so paths can be appended directly, e.g. `{url}/reset`).
19///
20/// This returns the first origin listed for the frontend in
21/// [`crate::env::app_meta`]. `frontend` must match the exact name declared in
22/// `encore.frontend.json` or the hosting service (for example, `web_app` does
23/// not match `web-app`).
24///
25/// # Panics
26///
27/// Panics if no URL is configured for `frontend` — either because the app has
28/// no frontend with that name or because the environment provides no origins
29/// for it.
30pub fn url(frontend: &str) -> &'static str {
31 let meta = crate::runtime::core().app_meta();
32 let origins = meta.frontend(frontend).map(|fe| fe.origins.as_slice());
33 crate::gateways::first_origin(origins).unwrap_or_else(|| {
34 panic!(
35 "encore: no URL configured for frontend {frontend:?}: either the app has no \
36 frontend with that name, or this environment provides no origins for it"
37 )
38 })
39}