use super::route_prelude::*;

/// Route to serve files from http/https URLs over Gemini
pub async fn route_www_get(ctx: RouteContext) -> Response {
    let Some(query) = decode_query(&ctx) else {
        return Response::input("URL");
    };

    let Ok(url) = Url::parse(&query) else {
        return Response::temporary_failure("Invalid URL");
    };

    match ehttp::fetch_async(ehttp::Request::get(url)).await {
        Ok(resp) => Response::binary_success_auto(&resp.bytes),
        Err(err) => {
            Response::temporary_failure(format!("Failed to fetch file: {err}"))
        }
    }
}
