Metadata
Get information about the app and the environment it runs in
Encore tries to provide a cloud-agnostic environment, but sometimes it's useful to know more about where your code is running. Encore.rs exposes metadata about the application, its environment, build, and deployment through encore::env::app_meta(), also available as encore::app_meta().
Application metadata
app_meta() returns a &'static AppMeta. It's computed once, when the runtime is initialized, so repeated calls are free.
let md = encore::app_meta();
println!("running {} in {}", md.app_id, md.environment.name);
AppMeta has these fields:
Environment
md.environment is an EnvironmentMeta with:
name: the environment's name. Forencore runit'slocal, and forencore testit'stest.r#type: anEnvironmentType, one ofProduction,Development,Ephemeral(such as preview environments), orTest.encore runreportsDevelopment.cloud: aCloudProvider, one ofAWS,GCP,Azure,Encore, orLocal. Locally it'sLocal.
These enums don't implement PartialEq, so compare them with matches! or a match:
use encore::env::EnvironmentType;
let is_prod = matches!(encore::app_meta().environment.r#type, EnvironmentType::Production);
Gateways
An app can have several gateways, so there's no single base URL. Look up the one you mean by name; each GatewayMeta has a name and a base_url:
if let Some(gw) = encore::app_meta().gateway("api") {
println!("api is served at {}", gw.base_url);
}
A process that no gateway serves, such as a worker, has an empty gateways map.
Example use cases
Using cloud-specific services
Encore doesn't natively support every service each cloud offers. Using the environment's cloud provider, you can choose an implementation per cloud. For example, to write audit logs to a data warehouse:
use encore::env::CloudProvider;
async fn audit(action: &str, user_id: &str) -> Result<(), AuditError> {
match encore::app_meta().environment.cloud {
CloudProvider::AWS => write_to_redshift(action, user_id).await,
CloudProvider::GCP => write_to_bigquery(action, user_id).await,
CloudProvider::Local => write_to_file(action, user_id).await,
CloudProvider::Azure | CloudProvider::Encore => Err(AuditError::Unsupported),
}
}
Checking the environment type
When building a signup flow, you may want to skip email verification during development and in tests:
use encore::env::EnvironmentType;
async fn finish_signup(user_id: &str) -> Result<(), SignupError> {
match encore::app_meta().environment.r#type {
EnvironmentType::Development | EnvironmentType::Test => {
mark_email_verified(user_id).await
}
_ => send_verification_email(user_id).await,
}
}
For values that differ per environment, consider configuration instead: overlay files per environment type or environment name keep the differences out of your code.