Runtime configuration

The same build of the app can be started in different environments. Pass the settings when starting the server:

vertigo serve --env api_url=https://api.example.com --env feature_x=on

and read them in the app with env, which works the same during server-side rendering and in the browser:

    use vertigo::{dom, get_driver, AutoJsJson, RequestBuilder};

    #[derive(AutoJsJson, Clone, PartialEq)]
    pub struct Status {
        version: String,
    }

    // Started with: vertigo serve --env api_url=https://api.example.com
    let api_url = get_driver()
        .env("api_url")
        .unwrap_or_else(|| "/api".to_string());

    let status = RequestBuilder::get(format!("{api_url}/status"))
        .ttl_minutes(1)
        .lazy_cache(|status, body| {
            if status == 200 {
                Some(body.into::<Status>())
            } else {
                None
            }
        });

    let version = status.render(|status| dom! { <span>{&status.version}</span> });

    dom! {
        <p>"API version: " {version}</p>
    }

The values are passed to the browser inside the rendered HTML, so they're visible to anyone. Never put secrets there.

The same --env option works for vertigo watch during development.

Proxy

Another way to avoid hardcoding the API address is serving the API from the same origin as the app. The server can forward requests for a path to another server:

vertigo serve --proxy /api=http://127.0.0.1:8080/api

Then the app can just use /api/... URLs. During server-side rendering such relative URLs are resolved against the address of the server itself, so they go through the same proxy. If the server should reach the API under a different address, set it with --ssr-fetch-base http://10.0.0.5:8080.