From db83d0adb0de24c7beb00f5778d032757d4fd30b Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 5 Jan 2025 22:35:06 +0800 Subject: [PATCH] cors and LISTEN env --- src/main.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4d33fca..1d3653a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,11 @@ async fn main() -> anyhow::Result<()> { let app = axum::Router::new() .fallback(get(api_proxy_image)) .with_state(downloader); - let listener = tokio::net::TcpListener::bind("0.0.0.0:2999").await?; + + let listen = std::env::var("LISTEN").unwrap_or("0.0.0.0:2999".to_string()); + println!("listening on: {}", listen); + let listener = tokio::net::TcpListener::bind(&listen).await?; + axum::serve(listener, app).await?; Ok(()) @@ -140,6 +144,12 @@ async fn api_proxy_image( Ok(Response::builder() .header("Content-Type", "image/webp") + .header("Cache-Control", "public, max-age=31536000") + .header("Access-Control-Allow-Origin", "*") + .header("Access-Control-Allow-Methods", "GET") + .header("Access-Control-Allow-Headers", "Content-Type") + .header("Access-Control-Max-Age", "86400") + .header("X-Original-Url", target_url) .header("X-Original-Content-Type", ori_content_type) .header("X-Original-Size", original_size.to_string()) .header("X-Original-Width", ori_width.to_string())