diff --git a/rupring/src/application_properties.rs b/rupring/src/application_properties.rs index 70adebf..95f32f0 100644 --- a/rupring/src/application_properties.rs +++ b/rupring/src/application_properties.rs @@ -27,6 +27,9 @@ | server.compression.min-response-size | The minimum response size to compress. (byte) | 2048 | | server.compression.algorithm | The compression algorithm to use. (gzip,deflate) | gzip | | server.thread.limit | The thread limit to use. | None(max) | +| banner.enabled | Whether to enable the banner. | true | +| banner.location | The location of the banner file. | None | +| banner.charset | The charset of the banner file. (UTF-8, UTF-16) | UTF-8 | */ use std::collections::HashMap; @@ -35,6 +38,7 @@ use std::collections::HashMap; pub struct ApplicationProperties { pub server: Server, pub environment: String, + pub banner: Banner, pub etc: HashMap, } @@ -45,6 +49,7 @@ impl Default for ApplicationProperties { server: Server::default(), environment: "dev".to_string(), etc: HashMap::new(), + banner: Banner::default(), } } } @@ -107,6 +112,23 @@ impl Default for Compression { } } +#[derive(Debug, PartialEq, Clone)] +pub struct Banner { + pub enabled: bool, + pub charset: String, + pub location: Option, +} + +impl Default for Banner { + fn default() -> Self { + Banner { + enabled: true, + charset: "UTF-8".to_string(), + location: None, + } + } +} + #[derive(Debug, PartialEq, Clone)] pub enum ShutdownType { Immediate, @@ -174,6 +196,7 @@ impl ApplicationProperties { let mut server = Server::default(); let mut environment = "dev".to_string(); let mut etc = HashMap::new(); + let mut banner = Banner::default(); let mut key_values = HashMap::new(); @@ -251,6 +274,15 @@ impl ApplicationProperties { "environment" => { environment = value.to_string(); } + "banner.enabled" => { + banner.enabled = value.parse::().unwrap_or(true); + } + "banner.location" => { + banner.location = Some(value.to_string()); + } + "banner.charset" => { + banner.charset = value.to_string(); + } _ => { etc.insert(key, value); } @@ -261,6 +293,7 @@ impl ApplicationProperties { server, etc, environment, + banner, } } } @@ -300,6 +333,7 @@ mod tests { }, etc: HashMap::new(), environment: "dev".to_string(), + ..Default::default() }, before: || { remove_all_env(); @@ -321,6 +355,7 @@ mod tests { }, environment: "dev".to_string(), etc: HashMap::from([("foo.bar".to_string(), "hello".to_string())]), + ..Default::default() }, before: || { remove_all_env(); @@ -341,6 +376,7 @@ mod tests { }, environment: "dev".to_string(), etc: HashMap::from([("asdf.fdsa".to_string(), "!!".to_string())]), + ..Default::default() }, before: || { remove_all_env(); @@ -362,6 +398,7 @@ mod tests { }, environment: "dev".to_string(), etc: HashMap::new(), + ..Default::default() }, before: || { remove_all_env(); @@ -382,6 +419,7 @@ mod tests { }, environment: "dev".to_string(), etc: HashMap::new(), + ..Default::default() }, before: || { remove_all_env(); @@ -402,6 +440,7 @@ mod tests { }, environment: "dev".to_string(), etc: HashMap::new(), + ..Default::default() }, before: || { remove_all_env(); @@ -423,6 +462,7 @@ mod tests { }, environment: "prod".to_string(), etc: HashMap::new(), + ..Default::default() }, before: || { remove_all_env(); diff --git a/rupring/src/core/banner.rs b/rupring/src/core/banner.rs index 1928089..5e0e673 100644 --- a/rupring/src/core/banner.rs +++ b/rupring/src/core/banner.rs @@ -1,12 +1,42 @@ -const RUPRING_TEXT: &'static str = r#",------. ,--. +use crate::application_properties::ApplicationProperties; + +const DEFAULT_BANNER_TEXT: &'static str = r#",------. ,--. | .--. ',--.,--. ,---. ,--.--.`--',--,--, ,---. | '--'.'| || || .-. || .--',--.| \| .-. | | |\ \ ' '' '| '-' '| | | || || |' '-' ' `--' '--' `----' | |-' `--' `--'`--''--'.`- / `--' `---' "#; -pub fn print_banner() { - println!("{}", RUPRING_TEXT); +pub fn print_banner(application_properties: &ApplicationProperties) { + if !application_properties.banner.enabled { + return; + } + + if let Some(location) = &application_properties.banner.location { + let bytes = std::fs::read(location).expect("Failed to find banner file"); + + let text = match application_properties + .banner + .charset + .to_uppercase() + .as_str() + { + "UTF-8" => String::from_utf8(bytes).unwrap_or_default(), + "UTF-16" => { + let utf16_bytes = bytes + .chunks(2) + .map(|b| u16::from_le_bytes([b[0], b[1]])) + .collect::>(); + String::from_utf16(&utf16_bytes).unwrap_or_default() + } + _ => String::from_utf8(bytes).unwrap_or_default(), + }; + + println!("{}", text); + } else { + println!("{}", DEFAULT_BANNER_TEXT); + } + print_app_info(); println!(""); } diff --git a/rupring/src/core/mod.rs b/rupring/src/core/mod.rs index 0ebff06..e961a5c 100644 --- a/rupring/src/core/mod.rs +++ b/rupring/src/core/mod.rs @@ -52,7 +52,7 @@ pub async fn run_server( } // 3. ready, set, go! - banner::print_banner(); + banner::print_banner(&application_properties); let socket_address = make_address(&application_properties)?; @@ -163,7 +163,7 @@ pub async fn run_server_on_aws_lambda( } // 3. ready, set, go! - banner::print_banner(); + banner::print_banner(&application_properties); let application_properties = Arc::new(application_properties); diff --git a/rupring_example/Cargo.toml b/rupring_example/Cargo.toml index b7b4c39..b9aeea9 100644 --- a/rupring_example/Cargo.toml +++ b/rupring_example/Cargo.toml @@ -6,7 +6,7 @@ default-run = "example" [dependencies] mockall = "0.13.1" -rupring={ version = "0.12.0", path="../rupring", features=["full"] } +rupring={ version = "0.12.2", path="../rupring", features=["full"] } serde = { version="1.0.193", features=["derive"] } [[bin]]