Skip to content

Commit

Permalink
setting app port defaults in any case and default log level to INFO
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardcl committed Jan 12, 2024
1 parent 54789ec commit f310e86
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions be-rust-axum/rust-template/src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ impl Settings {
/// Creates/loads the `Settings` with the configuration parameters required for the app
pub fn load() -> Self {
Settings {
port: std::env::var("PORT")
.unwrap_or("8080".to_owned())
.parse::<u16>()
.unwrap(),
log_level: std::env::var("LOG_LEVEL").unwrap_or("DEBUG".to_owned()),
port: app_port(),
log_level: std::env::var("LOG_LEVEL").unwrap_or("INFO".to_owned()),
}
}

Expand All @@ -45,3 +42,21 @@ impl Settings {
}
}
}

fn app_port() -> u16 {
std::env::var("PORT").map_or_else(
|e| {
println!("No PORT provided ({}), setting 8080", e);
8080
},
|v| {
v.parse::<u16>().map_or_else(
|e| {
println!("PORT value is not u16 parseable ({}), setting 8080", e);
8080
},
|v| v,
)
},
)
}

0 comments on commit f310e86

Please sign in to comment.