Move load_config to Config::from_env
This commit is contained in:
@@ -201,32 +201,34 @@ pub struct Config {
|
||||
pub server: ServerConfig,
|
||||
}
|
||||
|
||||
/// Parse configuration from the expected files and environment variables
|
||||
pub fn load_config() -> Result<Config, config::ConfigError> {
|
||||
use config::{Environment, File};
|
||||
impl Config {
|
||||
/// Parse configuration from the expected files and environment variables
|
||||
pub fn from_env() -> Result<Self, config::ConfigError> {
|
||||
use config::{Environment, File};
|
||||
|
||||
let pkg_name = env!("CARGO_PKG_NAME");
|
||||
let pkg_name = env!("CARGO_PKG_NAME");
|
||||
|
||||
config::Config::builder()
|
||||
.set_default(
|
||||
"server.port",
|
||||
dioxus::cli_config::server_port().unwrap_or(8080),
|
||||
)?
|
||||
.set_default(
|
||||
"server.host",
|
||||
dioxus::cli_config::server_ip()
|
||||
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED))
|
||||
.to_string(),
|
||||
)?
|
||||
.set_default("auth.open_signup", false)?
|
||||
.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}")).required(false))
|
||||
.add_source(File::with_name("config").required(false))
|
||||
.add_source(Environment::with_prefix(pkg_name).separator("_"))
|
||||
.build()?
|
||||
.try_deserialize()
|
||||
config::Config::builder()
|
||||
.set_default(
|
||||
"server.port",
|
||||
dioxus::cli_config::server_port().unwrap_or(8080),
|
||||
)?
|
||||
.set_default(
|
||||
"server.host",
|
||||
dioxus::cli_config::server_ip()
|
||||
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED))
|
||||
.to_string(),
|
||||
)?
|
||||
.set_default("auth.open_signup", false)?
|
||||
.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}")).required(false))
|
||||
.add_source(File::with_name("config").required(false))
|
||||
.add_source(Environment::with_prefix(pkg_name).separator("_"))
|
||||
.build()?
|
||||
.try_deserialize()
|
||||
}
|
||||
}
|
||||
|
||||
/// Provide a sane default for the public path, using the same sources as Dioxus does internally.
|
||||
|
||||
@@ -10,7 +10,7 @@ use tower_http::services::ServeFile;
|
||||
use crate::App;
|
||||
use crate::app::LOGO_ICO;
|
||||
use crate::server::{
|
||||
auth::build_auth_layer, config, database, key_val_store,
|
||||
auth::build_auth_layer, config::Config, database, key_val_store,
|
||||
require_auth_mw::require_auth_middleware,
|
||||
};
|
||||
use crate::util::error::{Contextualize, Error, ErrorType, Result};
|
||||
@@ -21,7 +21,7 @@ pub async fn main() -> Result<std::convert::Infallible> {
|
||||
}
|
||||
|
||||
tracing::debug!("Loading configuration...");
|
||||
let config = config::load_config()
|
||||
let config = Config::from_env()
|
||||
.map_err(|e| Error::message_here(e.to_string()))
|
||||
.err_context("Failed to load config")?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user