|
| 1 | +use axum::{ |
| 2 | + body::Body, |
| 3 | + http::{HeaderMap, Method, StatusCode}, |
| 4 | + response::IntoResponse, |
| 5 | + Json, |
| 6 | +}; |
| 7 | +use bytes::Bytes; |
| 8 | +use pangolin_store::CatalogStore; |
| 9 | +use pangolin_core::model::CatalogType; |
| 10 | +use std::sync::Arc; |
| 11 | +use crate::federated_proxy::FederatedCatalogProxy; |
| 12 | + |
| 13 | +pub mod config; |
| 14 | +pub mod namespaces; |
| 15 | +pub mod tables; |
| 16 | +pub mod types; |
| 17 | + |
| 18 | +// Re-export types for convenience |
| 19 | +pub use types::*; |
| 20 | +pub type AppState = std::sync::Arc<dyn pangolin_store::CatalogStore + Send + Sync>; |
| 21 | + |
| 22 | +/// Helper function to check if a catalog is federated and forward the request if so |
| 23 | +pub async fn check_and_forward_if_federated( |
| 24 | + store: &Arc<dyn CatalogStore + Send + Sync>, |
| 25 | + tenant_id: uuid::Uuid, |
| 26 | + catalog_name: &str, |
| 27 | + method: Method, |
| 28 | + path: &str, |
| 29 | + body: Option<Bytes>, |
| 30 | + headers: HeaderMap, |
| 31 | +) -> Option<axum::response::Response> { |
| 32 | + // Get the catalog |
| 33 | + let catalog = match store.get_catalog(tenant_id, catalog_name.to_string()).await { |
| 34 | + Ok(Some(c)) => c, |
| 35 | + Ok(None) => return None, // Catalog not found, let handler deal with it |
| 36 | + Err(_) => return None, |
| 37 | + }; |
| 38 | + |
| 39 | + // Check if it's federated |
| 40 | + if catalog.catalog_type == CatalogType::Federated { |
| 41 | + if let Some(config) = catalog.federated_config { |
| 42 | + let proxy = FederatedCatalogProxy::new(); |
| 43 | + match proxy.forward_request(&config, method, path, body, headers).await { |
| 44 | + Ok(response) => Some(response), |
| 45 | + Err(e) => Some(( |
| 46 | + StatusCode::BAD_GATEWAY, |
| 47 | + Json(serde_json::json!({"error": format!("Federated catalog error: {}", e)})), |
| 48 | + ).into_response()), |
| 49 | + } |
| 50 | + } else { |
| 51 | + Some(( |
| 52 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 53 | + Json(serde_json::json!({"error": "Federated catalog missing configuration"})), |
| 54 | + ).into_response()) |
| 55 | + } |
| 56 | + } else { |
| 57 | + None // Not federated, continue with local handling |
| 58 | + } |
| 59 | +} |
0 commit comments