Compare commits

...

2 Commits

3 changed files with 67 additions and 2 deletions

2
Cargo.lock generated
View File

@ -838,7 +838,7 @@ dependencies = [
[[package]]
name = "lonk"
version = "0.1.0"
version = "1.0.0"
dependencies = [
"argh",
"async-object-pool",

View File

@ -1,6 +1,6 @@
[package]
name = "lonk"
version = "0.1.0"
version = "1.0.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -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<usize>,
/// 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(),
}
}
}