| 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; |
|
|
| |
| #[derive(Serialize)] |
| pub struct ProxyStatus { |
| pub running: bool, |
| pub port: u16, |
| pub base_url: String, |
| #[serde(rename = "active_accounts")] |
| pub active_accounts: usize, |
| } |
|
|
| |
| |
| 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, |
| port, |
| base_url, |
| active_accounts: account_index.accounts.len(), |
| }; |
|
|
| Ok(ApiResponse::success(status)) |
| } |
|
|
| |
| pub async fn update_model_mapping( |
| Json(config): Json<ProxyConfig>, |
| ) -> Result<Json<ApiResponse<()>>, AppError> { |
| |
| 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(())) |
| } |
|
|
| |
| |
| |
| pub async fn restart_proxy( |
| Json(config): Json<ProxyConfig>, |
| ) -> Result<Json<ApiResponse<()>>, AppError> { |
| |
| 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(())) |
| } |
|
|
| |
| pub async fn generate_api_key() -> Result<Json<ApiResponse<String>>, AppError> { |
| let new_key = format!("ag-{}", Uuid::new_v4().to_string().replace("-", "")); |
|
|
| |
| 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)) |
| } |
|
|