diff --git a/src/config/app_config.rs b/src/config/app_config.rs index 278cd80e..d1cc05b2 100644 --- a/src/config/app_config.rs +++ b/src/config/app_config.rs @@ -74,9 +74,9 @@ impl AppConfig { } else { Environment::new()? }; - let environment_str: &str = environment.into(); + let environment_str: &str = environment.clone().into(); - let config = Self::default_config() + let config = Self::default_config(environment) // Todo: allow other file formats? // Todo: allow splitting config into multiple files? .add_source(config::File::with_name("config/default.toml")) @@ -99,7 +99,7 @@ impl AppConfig { #[cfg(test)] #[cfg_attr(coverage_nightly, coverage(off))] pub(crate) fn test(config_str: Option<&str>) -> RoadsterResult { - let config = Self::default_config() + let config = Self::default_config(Environment::Test) .add_source(config::File::from_str( config_str.unwrap_or( r#" @@ -146,7 +146,9 @@ impl AppConfig { } #[allow(clippy::let_and_return)] - fn default_config() -> ConfigBuilder { + fn default_config( + #[allow(unused_variables)] environment: Environment, + ) -> ConfigBuilder { let config = Config::builder() .add_source(config::File::from_str( include_str!("default.toml"), @@ -155,7 +157,13 @@ impl AppConfig { .add_source(crate::config::tracing::default_config()); #[cfg(feature = "http")] - let config = config.add_source(crate::config::service::http::default_config()); + let config = { + let config = config.add_source(crate::config::service::http::default_config()); + let config = crate::config::service::http::default_config_per_env(environment) + .into_iter() + .fold(config, |config, source| config.add_source(source)); + config + }; #[cfg(feature = "grpc")] let config = config.add_source(crate::config::service::grpc::default_config()); diff --git a/src/config/service/http/default.toml b/src/config/service/http/config/default.toml similarity index 99% rename from src/config/service/http/default.toml rename to src/config/service/http/config/default.toml index 7d2dd153..d3c54f76 100644 --- a/src/config/service/http/default.toml +++ b/src/config/service/http/config/default.toml @@ -42,7 +42,6 @@ limit = "5 MB" priority = -9950 [service.http.middleware.request-response-logging] -enable = false priority = 0 # Initializers diff --git a/src/config/service/http/config/production.toml b/src/config/service/http/config/production.toml new file mode 100644 index 00000000..0377c0ae --- /dev/null +++ b/src/config/service/http/config/production.toml @@ -0,0 +1,2 @@ +[service.http.middleware.request-response-logging] +enable = false \ No newline at end of file diff --git a/src/config/service/http/mod.rs b/src/config/service/http/mod.rs index afa394bf..5db08c36 100644 --- a/src/config/service/http/mod.rs +++ b/src/config/service/http/mod.rs @@ -1,3 +1,4 @@ +use crate::config::environment::Environment; use crate::config::service::common::address::Address; use crate::config::service::http::initializer::Initializer; use crate::config::service::http::middleware::Middleware; @@ -11,7 +12,17 @@ pub mod initializer; pub mod middleware; pub fn default_config() -> config::File { - config::File::from_str(include_str!("default.toml"), FileFormat::Toml) + config::File::from_str(include_str!("config/default.toml"), FileFormat::Toml) +} + +pub(crate) fn default_config_per_env( + environment: Environment, +) -> Option> { + let config = match environment { + Environment::Production => Some(include_str!("config/production.toml")), + _ => None, + }; + config.map(|c| config::File::from_str(c, FileFormat::Toml)) } #[derive(Debug, Clone, Validate, Serialize, Deserialize)] diff --git a/src/config/snapshots/roadster__config__app_config__tests__test.snap b/src/config/snapshots/roadster__config__app_config__tests__test.snap index 628086e9..0441c2f3 100644 --- a/src/config/snapshots/roadster__config__app_config__tests__test.snap +++ b/src/config/snapshots/roadster__config__app_config__tests__test.snap @@ -82,7 +82,6 @@ preset = 'restrictive' max-age = 3600000 [service.http.middleware.request-response-logging] -enable = false priority = 0 [service.http.initializer] diff --git a/src/service/http/middleware/snapshots/roadster__service__http__middleware__default__tests__default_middleware@case_2.snap b/src/service/http/middleware/snapshots/roadster__service__http__middleware__default__tests__default_middleware@case_2.snap index 5b9a2efd..76e2a793 100644 --- a/src/service/http/middleware/snapshots/roadster__service__http__middleware__default__tests__default_middleware@case_2.snap +++ b/src/service/http/middleware/snapshots/roadster__service__http__middleware__default__tests__default_middleware@case_2.snap @@ -8,6 +8,7 @@ expression: middleware 'propagate-request-id', 'request-body-size-limit', 'request-decompression', + 'request-response-logging', 'sensitive-request-headers', 'sensitive-response-headers', 'set-request-id', diff --git a/src/service/http/middleware/tracing/req_res_logging.rs b/src/service/http/middleware/tracing/req_res_logging.rs index 2dfcc9e7..fba7236a 100644 --- a/src/service/http/middleware/tracing/req_res_logging.rs +++ b/src/service/http/middleware/tracing/req_res_logging.rs @@ -64,7 +64,7 @@ where async fn log_req_res_bodies(request: Request, next: Next) -> Result { // Log the request body let (parts, body) = request.into_parts(); - let bytes = log_body(body, "request").await?; + let bytes = log_body(body, true).await?; let request = Request::from_parts(parts, Body::from(bytes)); // Handle the request @@ -72,14 +72,16 @@ async fn log_req_res_bodies(request: Request, next: Next) -> Result Result { +const MAX: usize = 1000; + +async fn log_body(body: Body, req: bool) -> Result { // This only works if the body is not a long-running stream let bytes = body .collect() @@ -87,7 +89,18 @@ async fn log_body(body: Body, msg: &str) -> Result { .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())? .to_bytes(); - debug!(body = ?bytes, msg); + let body = if bytes.len() > MAX { + let slice = bytes.slice(0..MAX); + format!("{slice:?}...[truncated]") + } else { + format!("{bytes:?}") + }; + + if req { + debug!(body, "request"); + } else { + debug!(body, "response"); + } Ok(bytes) }