Set attribute deny_unknown_fields for Lemmy config (#2852)

With this attribute, Lemmy will throw an error and exit if any
invalid entry is found in the config file. I think can be useful
to notice typos or keys that were removed or renamed in an upgrade.
Currently you wouldnt notice these at all unless you manually
compare the config file with settings that are listed in documentation.

This should be considered a breaking change.

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
This commit is contained in:
Nutomic 2023-05-12 02:12:12 +02:00 committed by GitHub
parent 32a5567cbf
commit af5175a282
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 7 deletions

View file

@ -80,7 +80,7 @@ impl Debug for LemmyError {
f.debug_struct("LemmyError") f.debug_struct("LemmyError")
.field("message", &self.message) .field("message", &self.message)
.field("inner", &self.inner) .field("inner", &self.inner)
.field("context", &"SpanTrace") .field("context", &self.context)
.finish() .finish()
} }
} }

View file

@ -14,8 +14,9 @@ pub mod structs;
static DEFAULT_CONFIG_FILE: &str = "config/config.hjson"; static DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
pub static SETTINGS: Lazy<Settings> = pub static SETTINGS: Lazy<Settings> = Lazy::new(|| {
Lazy::new(|| Settings::init().expect("Failed to load settings file")); Settings::init().expect("Failed to load settings file, see documentation (https://join-lemmy.org/docs/en/administration/configuration.html)")
});
static WEBFINGER_REGEX: Lazy<Regex> = Lazy::new(|| { static WEBFINGER_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(&format!( Regex::new(&format!(
"^acct:([a-zA-Z0-9_]{{3,}})@{}$", "^acct:([a-zA-Z0-9_]{{3,}})@{}$",
@ -53,11 +54,11 @@ impl Settings {
) )
} }
pub fn get_config_location() -> String { fn get_config_location() -> String {
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string()) env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string())
} }
pub fn read_config_file() -> Result<String, Error> { fn read_config_file() -> Result<String, Error> {
fs::read_to_string(Self::get_config_location()) fs::read_to_string(Self::get_config_location())
} }

View file

@ -42,7 +42,7 @@ pub struct Settings {
} }
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(default)] #[serde(default, deny_unknown_fields)]
pub struct PictrsConfig { pub struct PictrsConfig {
/// Address where pictrs is available (for image hosting) /// Address where pictrs is available (for image hosting)
#[default(Url::parse("http://localhost:8080").expect("parse pictrs url"))] #[default(Url::parse("http://localhost:8080").expect("parse pictrs url"))]
@ -55,7 +55,7 @@ pub struct PictrsConfig {
} }
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(default)] #[serde(default, deny_unknown_fields)]
pub struct DatabaseConfig { pub struct DatabaseConfig {
/// Username to connect to postgres /// Username to connect to postgres
#[default("lemmy")] #[default("lemmy")]
@ -78,6 +78,7 @@ pub struct DatabaseConfig {
} }
#[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)] #[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)]
#[serde(deny_unknown_fields)]
pub struct EmailConfig { pub struct EmailConfig {
/// Hostname and port of the smtp server /// Hostname and port of the smtp server
#[doku(example = "localhost:25")] #[doku(example = "localhost:25")]
@ -96,6 +97,7 @@ pub struct EmailConfig {
} }
#[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
#[serde(deny_unknown_fields)]
pub struct SetupConfig { pub struct SetupConfig {
/// Username for the admin user /// Username for the admin user
#[doku(example = "admin")] #[doku(example = "admin")]