Skip to content

Commit

Permalink
Merge pull request #189 from myyrakle/feat/#172
Browse files Browse the repository at this point in the history
[#172] 배너 관련 기능 재작성
  • Loading branch information
myyrakle authored Dec 15, 2024
2 parents 6042862 + 083f23a commit 60a96a3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
40 changes: 40 additions & 0 deletions rupring/src/application_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +38,7 @@ use std::collections::HashMap;
pub struct ApplicationProperties {
pub server: Server,
pub environment: String,
pub banner: Banner,

pub etc: HashMap<String, String>,
}
Expand All @@ -45,6 +49,7 @@ impl Default for ApplicationProperties {
server: Server::default(),
environment: "dev".to_string(),
etc: HashMap::new(),
banner: Banner::default(),
}
}
}
Expand Down Expand Up @@ -107,6 +112,23 @@ impl Default for Compression {
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct Banner {
pub enabled: bool,
pub charset: String,
pub location: Option<String>,
}

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,
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -251,6 +274,15 @@ impl ApplicationProperties {
"environment" => {
environment = value.to_string();
}
"banner.enabled" => {
banner.enabled = value.parse::<bool>().unwrap_or(true);
}
"banner.location" => {
banner.location = Some(value.to_string());
}
"banner.charset" => {
banner.charset = value.to_string();
}
_ => {
etc.insert(key, value);
}
Expand All @@ -261,6 +293,7 @@ impl ApplicationProperties {
server,
etc,
environment,
banner,
}
}
}
Expand Down Expand Up @@ -300,6 +333,7 @@ mod tests {
},
etc: HashMap::new(),
environment: "dev".to_string(),
..Default::default()
},
before: || {
remove_all_env();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -362,6 +398,7 @@ mod tests {
},
environment: "dev".to_string(),
etc: HashMap::new(),
..Default::default()
},
before: || {
remove_all_env();
Expand All @@ -382,6 +419,7 @@ mod tests {
},
environment: "dev".to_string(),
etc: HashMap::new(),
..Default::default()
},
before: || {
remove_all_env();
Expand All @@ -402,6 +440,7 @@ mod tests {
},
environment: "dev".to_string(),
etc: HashMap::new(),
..Default::default()
},
before: || {
remove_all_env();
Expand All @@ -423,6 +462,7 @@ mod tests {
},
environment: "prod".to_string(),
etc: HashMap::new(),
..Default::default()
},
before: || {
remove_all_env();
Expand Down
36 changes: 33 additions & 3 deletions rupring/src/core/banner.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<u16>>();
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!("");
}
Expand Down
4 changes: 2 additions & 2 deletions rupring/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion rupring_example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down

0 comments on commit 60a96a3

Please sign in to comment.