From 9a59da600eba7f52f5e0483846ae4d8af208add8 Mon Sep 17 00:00:00 2001 From: meeg_leeto Date: Thu, 12 May 2022 19:16:28 +0100 Subject: [PATCH] wip: added log rules to configuration file --- src/main.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5daaf70..aa6dbc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -138,6 +138,15 @@ mod conf { pub addr: ServeAddr, } + #[derive(Deserialize, Serialize, Debug, Clone)] + /// Configuration of logging by lonk. + pub struct LogRules { + /// Where to write error logs to. The file will be appended to. + error_log_file: PathBuf, + /// Where to write access ogs to. The file will be appended to. + access_log_file: PathBuf, + } + #[derive(Deserialize, Serialize, Debug, Clone)] /// Configuration struct. This struct is a typed representation of the /// configuration file, with each of the domain-specific configurations @@ -153,6 +162,8 @@ mod conf { pub version: Option, /// Configuration regarding the Redis database. pub db: DbConfig, + /// Configuration regarding logging. + pub log_rules: LogRules, /// Configuration regarding the types of (URL shorten) slugs produced. pub slug_rules: SlugRules, /// Configuration regarding where and how the HTTP server is served. @@ -171,6 +182,10 @@ mod conf { ServeFileNotExists(PathBuf), ServeDirNotDir(PathBuf), ServeDirNotExists(PathBuf), + BadAccessLogPath, + BadErrorLogPath, + AccessLogDirectoryNotExists(PathBuf), + ErrorLogDirectoryNotExists(PathBuf), } impl Config { @@ -210,6 +225,34 @@ mod conf { } } + // Check access and error log parent directories + // - Access log file + let canonical = self + .log_rules + .access_log_file + .canonicalize() + .map_err(|_| ConfigParseError::BadAccessLogPath)?; + if let Some(parent) = canonical.parent() { + if !parent.exists() { + return Err(ConfigParseError::AccessLogDirectoryNotExists( + parent.to_path_buf(), + )); + } + } + // - Error log file + let canonical = self + .log_rules + .error_log_file + .canonicalize() + .map_err(|_| ConfigParseError::BadErrorLogPath)?; + if let Some(parent) = canonical.parent() { + if !parent.exists() { + return Err(ConfigParseError::ErrorLogDirectoryNotExists( + parent.to_path_buf(), + )); + } + } + Ok(self) } } @@ -271,6 +314,18 @@ mod conf { file.to_string_lossy() ) } + ConfigParseError::BadAccessLogPath => { + panic!("Access log path could not be parsed as a canonicalizable path.") + } + ConfigParseError::BadErrorLogPath => { + panic!("Error log path could not be parsed as a canonicalizable path.") + } + ConfigParseError::AccessLogDirectoryNotExists(dir) => { + panic!("Access log file should have parent directory {}, but this directory does not exist.", dir.to_string_lossy()) + } + ConfigParseError::ErrorLogDirectoryNotExists(dir) => { + panic!("Error log file should have parent directory {}, but this directory does not exist.", dir.to_string_lossy()) + } } } } @@ -329,6 +384,15 @@ mod conf { } } + impl Default for LogRules { + fn default() -> Self { + Self { + error_log_file: "/etc/lonk/log/error.log".into(), + access_log_file: "/etc/lonk/log/access.log".into(), + } + } + } + impl Default for Config { fn default() -> Self { Self { @@ -336,6 +400,7 @@ mod conf { db: Default::default(), slug_rules: Default::default(), serve_rules: Default::default(), + log_rules: Default::default(), } } }