Environment
The env module provides simple access to environment variables.
Required Imports
Section titled “Required Imports”#[allow(warnings)]mod bindings;
use bindings::exports::mik::core::handler::{self, Guest, Response};use mik_sdk::prelude::*;The env module is included in mik_sdk::prelude::*.
Getting Variables
Section titled “Getting Variables”Optional Access
Section titled “Optional Access”use mik_sdk::env;
let value = env::get("DATABASE_URL"); // Option<String>
if let Some(url) = value { // Use the URL}With Default Value
Section titled “With Default Value”let port = env::get_or("PORT", "8080"); // Stringlet env = env::get_or("ENV", "development");Required Value
Section titled “Required Value”let secret = env::require("API_SECRET"); // String, panics if missingCommon Patterns
Section titled “Common Patterns”Configuration Loading
Section titled “Configuration Loading”struct Config { database_url: String, redis_url: Option<String>, log_level: String, max_connections: u32,}
fn load_config() -> Config { Config { database_url: env::require("DATABASE_URL"), redis_url: env::get("REDIS_URL"), log_level: env::get_or("LOG_LEVEL", "info"), max_connections: env::get_or("MAX_CONNECTIONS", "10") .parse() .unwrap_or(10), }}Feature Flags
Section titled “Feature Flags”fn is_feature_enabled(feature: &str) -> bool { let key = format!("FEATURE_{}", feature.to_uppercase()); env::get(&key) .map(|v| v == "true" || v == "1") .unwrap_or(false)}
fn handler(_req: &Request) -> Response { if is_feature_enabled("new_api") { // New implementation } else { // Old implementation } ok!({})}Service URLs
Section titled “Service URLs”fn get_service_url(service: &str) -> String { let key = format!("{}_URL", service.to_uppercase()); env::get_or(&key, &format!("http://{}:8080", service))}
fn call_service(service: &str) -> Response { let url = get_service_url(service); let response = fetch!(GET &url).send()?; ok!({ "status": response.status() })}Environment-Specific Behavior
Section titled “Environment-Specific Behavior”fn is_production() -> bool { env::get_or("ENV", "development") == "production"}
fn handler(_req: &Request) -> Response { if is_production() { // Production behavior } else { // Development behavior (e.g., more verbose responses) } ok!({})}API Summary
Section titled “API Summary”| Function | Returns | Description |
|---|---|---|
env::get(key) | Option<String> | Get variable, None if not set |
env::get_or(key, default) | String | Get variable or use default |
env::require(key) | String | Get variable, panic if not set |
Examples
Section titled “Examples”use mik_sdk::env;
// Optionallet optional = env::get("OPTIONAL_VAR");
// With defaultlet port = env::get_or("PORT", "3000");let host = env::get_or("HOST", "0.0.0.0");
// Required (panics if missing)let secret = env::require("SECRET_KEY");
// Parse to other typeslet timeout: u64 = env::get_or("TIMEOUT", "30") .parse() .unwrap_or(30);
let debug: bool = env::get_or("DEBUG", "false") == "true";Runtime Considerations
Section titled “Runtime Considerations”When running with wasmtime:
# Set variables for the componentwasmtime serve -S cli=y \ --env DATABASE_URL=postgres://localhost/db \ --env API_SECRET=mysecret \ service.wasm