feat: 支持多区域配置的Cloudflare Gateway URL

多区域支持:
- 将region改为regions字符串数组
- 支持多个区域: us-central1, us-east1, europe-west1
- 使用第一个区域作为默认区域
- 添加区域选择的详细日志

 Cloudflare Gateway URL格式:
- 完整的URL构建逻辑
- 格式: https://gateway.ai.cloudflare.com/v1/{project_id}/{gateway_id}/google-vertex-ai/v1/projects/{google_project_id}/locations/{region}/publishers/google/models
- 支持动态区域切换

 配置参数:
- cloudflare_project_id: 67720b647ff2b55cf37ba3ef9e677083
- cloudflare_gateway_id: bowong-dev
- google_project_id: gen-lang-client-0413414134
- regions: [us-central1, us-east1, europe-west1]

 日志增强:
- 显示构建的完整Gateway URL
- 显示当前使用的区域
- 显示所有可用区域列表

现在支持标准的Cloudflare Gateway多区域配置。
This commit is contained in:
imeepos 2025-07-14 13:33:15 +08:00
parent 37e75785da
commit da5bb8e43d
1 changed files with 32 additions and 2 deletions

View File

@ -17,6 +17,10 @@ pub struct GeminiConfig {
pub retry_delay: u64, pub retry_delay: u64,
pub temperature: f32, pub temperature: f32,
pub max_tokens: u32, pub max_tokens: u32,
pub cloudflare_project_id: String,
pub cloudflare_gateway_id: String,
pub google_project_id: String,
pub regions: Vec<String>,
} }
impl Default for GeminiConfig { impl Default for GeminiConfig {
@ -30,6 +34,14 @@ impl Default for GeminiConfig {
retry_delay: 2, retry_delay: 2,
temperature: 0.1, temperature: 0.1,
max_tokens: 2048, max_tokens: 2048,
cloudflare_project_id: "67720b647ff2b55cf37ba3ef9e677083".to_string(),
cloudflare_gateway_id: "bowong-dev".to_string(),
google_project_id: "gen-lang-client-0413414134".to_string(),
regions: vec![
"us-central1".to_string(),
"us-east1".to_string(),
"europe-west1".to_string(),
],
} }
} }
} }
@ -208,9 +220,27 @@ impl GeminiService {
let mut headers = std::collections::HashMap::new(); let mut headers = std::collections::HashMap::new();
headers.insert("Authorization".to_string(), format!("Bearer {}", access_token)); headers.insert("Authorization".to_string(), format!("Bearer {}", access_token));
headers.insert("Content-Type".to_string(), "application/json".to_string()); headers.insert("Content-Type".to_string(), "application/json".to_string());
// 使用第一个区域作为默认区域
let region = self.config.regions.first()
.unwrap_or(&"us-central1".to_string())
.clone();
// 构建Cloudflare Gateway URL
let gateway_url = format!(
"https://gateway.ai.cloudflare.com/v1/{}/{}/google-vertex-ai/v1/projects/{}/locations/{}/publishers/google/models",
self.config.cloudflare_project_id,
self.config.cloudflare_gateway_id,
self.config.google_project_id,
region
);
println!("🔗 构建的Cloudflare Gateway URL: {}", gateway_url);
println!("🌍 使用的区域: {}", region);
println!("📋 可用区域: {:?}", self.config.regions);
ClientConfig { ClientConfig {
gateway_url: self.config.base_url.clone(), gateway_url,
headers, headers,
} }
} }