Add public_path config option

This commit is contained in:
2026-06-30 21:08:05 -04:00
parent 69eb3a5e80
commit 8b00db60eb

View File

@@ -1,3 +1,5 @@
use std::path::PathBuf;
use serde::Deserialize; use serde::Deserialize;
/// Enable secure cookies by default only in release mode /// Enable secure cookies by default only in release mode
@@ -141,12 +143,18 @@ impl KeyValStoreConfig {
} }
} }
#[derive(Debug, Clone, Deserialize)]
pub struct ServerConfig {
pub public_path: PathBuf,
}
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
/// Top-level application configuration /// Top-level application configuration
pub struct Config { pub struct Config {
pub auth: AuthConfig, pub auth: AuthConfig,
pub database: DatabaseConfig, pub database: DatabaseConfig,
pub key_val_store: KeyValStoreConfig, pub key_val_store: KeyValStoreConfig,
pub server: ServerConfig,
} }
/// Parse configuration from the expected files and environment variables /// Parse configuration from the expected files and environment variables
@@ -159,6 +167,7 @@ pub fn load_config() -> Result<Config, config::ConfigError> {
.set_default("server.port", 8080)? .set_default("server.port", 8080)?
.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())?
.add_source(File::with_name(&format!("/etc/{pkg_name}/config")).required(false)) .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(&format!("/etc/{pkg_name}")).required(false))
.add_source(File::with_name("config").required(false)) .add_source(File::with_name("config").required(false))
@@ -166,3 +175,19 @@ pub fn load_config() -> Result<Config, config::ConfigError> {
.build()? .build()?
.try_deserialize() .try_deserialize()
} }
/// Provide a sane default for the public path, using the same sources as Dioxus does internally.
/// Checks the `DIOXUS_PUBLIC_PATH` environment variable, then tries relative to the path of this
/// executable
fn default_public_dir() -> Option<String> {
std::env::var("DIOXUS_PUBLIC_PATH").ok().or_else(|| {
std::env::current_exe().ok().and_then(|path| {
path.parent()
.expect("current executable path must have a parent")
.join("public")
.into_os_string()
.into_string()
.ok()
})
})
}