//! URL routing helpers used by the reverse proxy.
//!
//! Chooses a target base URL based on the request host and preserves the path.
//!
//!

use std::env::var;
fn get_target_base_url() -> String {
    var("TARGET_BASE_URL").unwrap_or_else(|_| "".to_string())
}

fn get_host_dexter() -> String {
    var("HOST_DEXTER").unwrap_or_else(|_| "".to_string())
}

fn get_target_base_url_dexter() -> String {
    var("TARGET_BASE_URL_DEXTER").unwrap_or_else(|_| "".to_string())
}

/// Determine the proxied target URL from the incoming `host` and request `path`.
pub fn determine_target_url(host: &str, path: &str) -> String {
    let host_dexter: String = get_host_dexter();
    let target_base_url_dexter: String = get_target_base_url_dexter();
    let target_base_url: String = get_target_base_url();

    if host.contains(&host_dexter) {
        format!("{}{}", target_base_url_dexter, path)
    } else {
        format!("{}{}", target_base_url, path)
    }
}
