|
| 1 | +use crate::{configs::Config, utils::merge_config_with_override}; |
| 2 | +use axum::{body::Body, extract::Request, http::StatusCode, response::Response}; |
| 3 | +use std::{ |
| 4 | + future::Future, |
| 5 | + pin::Pin, |
| 6 | + sync::Arc, |
| 7 | + task::{Context, Poll}, |
| 8 | +}; |
| 9 | +use tower::{Layer, Service}; |
| 10 | + |
| 11 | +fn create_error_response(message: &str) -> Response<Body> { |
| 12 | + Response::builder() |
| 13 | + .status(StatusCode::INTERNAL_SERVER_ERROR) |
| 14 | + .body(Body::from(message.to_string())) |
| 15 | + .unwrap_or_else(|_| { |
| 16 | + // Single fallback - no nested unwrap needed |
| 17 | + let mut response = Response::new(Body::from("Internal server error")); |
| 18 | + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; |
| 19 | + response |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +// HTTP middleware layer for adding config to request extensions |
| 24 | +#[derive(Clone)] |
| 25 | +pub struct HttpRequestExtensionsLayer { |
| 26 | + base_config: Arc<Config>, |
| 27 | +} |
| 28 | + |
| 29 | +#[allow(clippy::new_without_default)] |
| 30 | +impl HttpRequestExtensionsLayer { |
| 31 | + pub fn new(base_config: Arc<Config>) -> Self { |
| 32 | + Self { base_config } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl<S> Layer<S> for HttpRequestExtensionsLayer { |
| 37 | + type Service = HttpRequestExtensionsMiddleware<S>; |
| 38 | + |
| 39 | + fn layer(&self, inner: S) -> Self::Service { |
| 40 | + HttpRequestExtensionsMiddleware { |
| 41 | + inner, |
| 42 | + base_config: self.base_config.clone(), |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Clone)] |
| 48 | +pub struct HttpRequestExtensionsMiddleware<S> { |
| 49 | + inner: S, |
| 50 | + base_config: Arc<Config>, |
| 51 | +} |
| 52 | + |
| 53 | +impl<S> Service<Request<Body>> for HttpRequestExtensionsMiddleware<S> |
| 54 | +where |
| 55 | + S: Service<Request<Body>, Response = Response, Error = std::convert::Infallible> |
| 56 | + + Send |
| 57 | + + 'static, |
| 58 | + S::Future: Send + 'static, |
| 59 | +{ |
| 60 | + type Response = S::Response; |
| 61 | + type Error = S::Error; |
| 62 | + type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; |
| 63 | + |
| 64 | + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 65 | + self.inner.poll_ready(cx) |
| 66 | + } |
| 67 | + |
| 68 | + fn call(&mut self, mut req: Request<Body>) -> Self::Future { |
| 69 | + // Extract x-config-override header first |
| 70 | + let config_override = req |
| 71 | + .headers() |
| 72 | + .get("x-config-override") |
| 73 | + .and_then(|h| h.to_str().map(|s| s.to_owned()).ok()); |
| 74 | + |
| 75 | + // Only process config if override header is present |
| 76 | + match config_override { |
| 77 | + Some(override_str) => { |
| 78 | + // Merge override with default |
| 79 | + let new_config = match merge_config_with_override( |
| 80 | + Some(override_str), |
| 81 | + (*self.base_config).clone(), |
| 82 | + ) { |
| 83 | + Ok(cfg) => cfg, |
| 84 | + Err(e) => { |
| 85 | + let error_response = create_error_response(&format!( |
| 86 | + "Failed to merge config with override config: {e:?}" |
| 87 | + )); |
| 88 | + let fut = async move { Ok(error_response) }; |
| 89 | + return Box::pin(fut); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + // Insert merged config into extensions |
| 94 | + req.extensions_mut().insert(new_config); |
| 95 | + } |
| 96 | + None => { |
| 97 | + // No override header - insert base config |
| 98 | + req.extensions_mut().insert(Arc::clone(&self.base_config)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + let future = self.inner.call(req); |
| 103 | + Box::pin(async move { |
| 104 | + let response = future.await?; |
| 105 | + Ok(response) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments