use fastly::{Error, Request, Response};

/// The name of backend server associated with this service.
const BACKEND_PRODUCTION: &str = "origin_0";
const BACKEND_TEST: &str = "origin_1";

#[fastly::main]
fn main(mut req_production: Request) -> Result<Response, Error> {
    log_fastly::init_simple("my_log", log::LevelFilter::Info);
    fastly::log::set_panic_endpoint("my_log").unwrap();

    // Clone the request so we can send a copy to each backend. Note that this is potentially
    // expensive for large bodies, as the body must be buffered.
    let req_test = req_production.clone_with_body();

    // Send an asynchronous request to production and test backends
    let pending_req_production = req_production.send_async(BACKEND_PRODUCTION)?;
    let pending_req_test = req_test.send_async(BACKEND_TEST)?;

    // Wait for the responses from each backend
    let resp_production = pending_req_production.wait()?;
    let resp_test = pending_req_test.wait()?;

    let is_same_status = resp_production.get_status() == resp_test.get_status();
    let is_same_content_len =
        resp_production.get_content_length() == resp_test.get_content_length();

    if is_same_status && is_same_content_len {
        log::info!("Prod and test are the same");
    } else {
        log::info!("The production and test environments returned different content");
    }

    Ok(resp_production)
}