Initialize config defaults in separate function
All checks were successful
Push Workflows / rustfmt (push) Successful in 6s
Push Workflows / tailwind-build (push) Successful in 6s
Push Workflows / docs (push) Successful in 20s
Push Workflows / test (push) Successful in 24s
Push Workflows / clippy (push) Successful in 18s
Push Workflows / build (push) Successful in 47s
Push Workflows / nix-build (push) Successful in 5m16s

This commit is contained in:
2026-07-19 10:06:33 -04:00
parent b15b8b2ae1
commit 771b8e5ce4

View File

@@ -1,6 +1,7 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf; use std::path::PathBuf;
use config::{ConfigBuilder, ConfigError, builder::DefaultState};
use serde::Deserialize; use serde::Deserialize;
/// Enable secure cookies by default only in release mode /// Enable secure cookies by default only in release mode
@@ -203,11 +204,22 @@ pub struct Config {
impl Config { impl Config {
/// Parse configuration from the expected files and environment variables /// Parse configuration from the expected files and environment variables
pub fn from_env() -> Result<Self, config::ConfigError> { pub fn from_env() -> Result<Self, ConfigError> {
use config::{Environment, File}; use config::{Environment, File};
let pkg_name = env!("CARGO_PKG_NAME"); let pkg_name = env!("CARGO_PKG_NAME");
Self::defaults()?
.add_source(File::with_name(&format!("/etc/{pkg_name}/config")).required(false))
.add_source(File::with_name(&format!("/etc/{pkg_name}")).required(false))
.add_source(File::with_name("config").required(false))
.add_source(Environment::with_prefix(pkg_name).separator("_"))
.build()?
.try_deserialize()
}
/// Generate a `config::ConfigBuilder` from default values
fn defaults() -> Result<ConfigBuilder<DefaultState>, ConfigError> {
config::Config::builder() config::Config::builder()
.set_default( .set_default(
"server.port", "server.port",
@@ -221,13 +233,7 @@ impl Config {
)? )?
.set_default("auth.open_signup", false)? .set_default("auth.open_signup", false)?
.set_default("auth.cookies_secure", DEFAULT_COOKIES_SECURE)? .set_default("auth.cookies_secure", DEFAULT_COOKIES_SECURE)?
.set_default("server.public_path", default_public_dir())? .set_default("server.public_path", default_public_dir())
.add_source(File::with_name(&format!("/etc/{pkg_name}/config")).required(false))
.add_source(File::with_name(&format!("/etc/{pkg_name}")).required(false))
.add_source(File::with_name("config").required(false))
.add_source(Environment::with_prefix(pkg_name).separator("_"))
.build()?
.try_deserialize()
} }
} }