gemini / server /src /api /proxy.rs
yinming
feat: Antigravity API Proxy for HuggingFace Spaces
bbb1195
use axum::Json;
use serde::Serialize;
use uuid::Uuid;
use crate::proxy::config::ProxyConfig;
use crate::modules;
use crate::error::AppError;
use super::config::ApiResponse;
/// Proxy status response
#[derive(Serialize)]
pub struct ProxyStatus {
pub running: bool,
pub port: u16,
pub base_url: String,
#[serde(rename = "active_accounts")]
pub active_accounts: usize,
}
/// Get proxy status
/// In cloud deployment mode, the proxy is always running
pub async fn get_proxy_status() -> Result<Json<ApiResponse<ProxyStatus>>, AppError> {
let _config = modules::load_app_config().map_err(AppError::Config)?;
let account_index = modules::load_account_index().map_err(AppError::Account)?;
let port = std::env::var("PORT")
.unwrap_or_else(|_| "7860".to_string())
.parse::<u16>()
.unwrap_or(7860);
let base_url = std::env::var("SPACE_HOST")
.map(|host| format!("https://{}", host))
.unwrap_or_else(|_| format!("http://localhost:{}", port));
let status = ProxyStatus {
running: true, // Always running in cloud mode
port,
base_url,
active_accounts: account_index.accounts.len(),
};
Ok(ApiResponse::success(status))
}
/// Update model mapping
pub async fn update_model_mapping(
Json(config): Json<ProxyConfig>,
) -> Result<Json<ApiResponse<()>>, AppError> {
// Load current config and update proxy section
let mut app_config = modules::load_app_config().map_err(AppError::Config)?;
app_config.proxy.anthropic_mapping = config.anthropic_mapping;
app_config.proxy.openai_mapping = config.openai_mapping;
app_config.proxy.custom_mapping = config.custom_mapping;
modules::save_app_config(&app_config).map_err(AppError::Config)?;
modules::logger::log_info("Model mapping updated");
Ok(ApiResponse::success(()))
}
/// Restart proxy service (reload configuration)
/// In cloud mode, this doesn't actually restart the server,
/// but signals that configuration has been updated
pub async fn restart_proxy(
Json(config): Json<ProxyConfig>,
) -> Result<Json<ApiResponse<()>>, AppError> {
// Load and update configuration
let mut app_config = modules::load_app_config().map_err(AppError::Config)?;
app_config.proxy = config;
modules::save_app_config(&app_config).map_err(AppError::Config)?;
modules::logger::log_info("Proxy configuration updated (restart not needed in cloud mode)");
Ok(ApiResponse::success(()))
}
/// Generate a new API key
pub async fn generate_api_key() -> Result<Json<ApiResponse<String>>, AppError> {
let new_key = format!("ag-{}", Uuid::new_v4().to_string().replace("-", ""));
// Update config with new key
let mut app_config = modules::load_app_config().map_err(AppError::Config)?;
app_config.proxy.api_key = new_key.clone();
modules::save_app_config(&app_config).map_err(AppError::Config)?;
modules::logger::log_info("New API key generated");
Ok(ApiResponse::success(new_key))
}