Skip to content

Commit 199d48a

Browse files
committed
Fix config logs handling
1 parent a7fd978 commit 199d48a

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

src/config.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ use tracing::{error, info};
55

66
use crate::models::{Players, StatusResponse, Version};
77

8+
fn default_false() -> bool {
9+
false
10+
}
11+
812
#[derive(Clone, Serialize, Deserialize)]
913
#[serde(default)]
1014
pub struct Config {
15+
// This field is used to determine if the config file was found
16+
#[serde(skip, default = "default_false")]
17+
default: bool,
18+
1119
pub debug: bool,
1220
pub host: String,
1321
pub port: u16,
@@ -18,6 +26,7 @@ pub struct Config {
1826
impl Default for Config {
1927
fn default() -> Self {
2028
Config {
29+
default: true,
2130
debug: false,
2231
host: "127.0.0.1".to_string(),
2332
port: 25565,
@@ -40,51 +49,57 @@ impl Default for Config {
4049
impl Config {
4150
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
4251
if !path.as_ref().exists() {
43-
info!("Config file not found, using default configuration.");
4452
return Ok(Config::default());
4553
}
4654

4755
let contents = fs::read_to_string(path)?;
48-
let mut config: Config = serde_yaml::from_str(&contents)
56+
let config: Config = serde_yaml::from_str(&contents)
4957
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
5058

51-
// Handle the favicon
52-
if let Some(favicon) = config.status.favicon {
53-
match Self::load_favicon_as_base64(favicon) {
54-
Ok(favicon_base64) => config.status.favicon = Some(favicon_base64),
59+
Ok(config)
60+
}
61+
62+
pub fn handle_logs(&self) {
63+
if self.default {
64+
info!("Config file not found, using default configuration.");
65+
}
66+
}
67+
68+
pub fn handle_favicon(&mut self) {
69+
if let Some(favicon) = &self.status.favicon {
70+
match Self::load_favicon_as_base64(favicon.to_string()) {
71+
Ok(favicon_base64) => self.status.favicon = Some(favicon_base64),
5572
Err(e) => {
5673
error!("Error loading favicon: {}", e);
57-
config.status.favicon = None;
74+
self.status.favicon = None;
5875
}
5976
}
6077
}
61-
62-
Ok(config)
6378
}
6479

6580
fn load_favicon_as_base64(favicon: String) -> Result<String, io::Error> {
6681
if favicon.starts_with("data:image/png;base64,") {
67-
Ok(favicon)
68-
} else {
69-
let favicon_path = Path::new(&favicon);
70-
if favicon_path.extension().and_then(|ext| ext.to_str()) != Some("png") {
71-
return Err(io::Error::new(
72-
io::ErrorKind::InvalidInput,
73-
"Favicon must be a PNG file",
74-
));
75-
}
76-
77-
if !favicon_path.exists() {
78-
return Err(io::Error::new(
79-
io::ErrorKind::NotFound,
80-
format!("Favicon file doesn't exist: {}", favicon_path.display()),
81-
));
82-
}
82+
return Ok(favicon);
83+
}
8384

84-
let favicon_bytes = fs::read(favicon)?;
85-
let favicon_base64 = base64_engine::STANDARD.encode(&favicon_bytes);
85+
let favicon_path = Path::new(&favicon);
86+
if favicon_path.extension().and_then(|ext| ext.to_str()) != Some("png") {
87+
return Err(io::Error::new(
88+
io::ErrorKind::InvalidInput,
89+
"Favicon must be a PNG file",
90+
));
91+
}
8692

87-
Ok(format!("data:image/png;base64,{}", favicon_base64))
93+
if !favicon_path.exists() {
94+
return Err(io::Error::new(
95+
io::ErrorKind::NotFound,
96+
format!("Favicon file doesn't exist: {}", favicon_path.display()),
97+
));
8898
}
99+
100+
let favicon_bytes = fs::read(favicon)?;
101+
let favicon_base64 = base64_engine::STANDARD.encode(&favicon_bytes);
102+
103+
Ok(format!("data:image/png;base64,{}", favicon_base64))
89104
}
90105
}

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tracing_subscriber::EnvFilter;
1212
#[tokio::main]
1313
async fn main() -> io::Result<()> {
1414
let config_path = env::var("CONFIG_PATH").unwrap_or_else(|_| "config.yml".to_string());
15-
let config = match Config::load(config_path) {
15+
let mut config = match Config::load(config_path) {
1616
Ok(config) => config,
1717
Err(e) => {
1818
panic!("Failed to load config: {} ({:?})", e, e.kind());
@@ -33,6 +33,9 @@ async fn main() -> io::Result<()> {
3333
.with_target(false)
3434
.init();
3535

36+
config.handle_logs();
37+
config.handle_favicon();
38+
3639
let listener = TcpListener::bind(format!("{}:{}", config.host, config.port)).await?;
3740
info!("Server listening on port {}", config.port);
3841

0 commit comments

Comments
 (0)