Compare commits
3 Commits
3816f3ef28
...
771b8e5ce4
| Author | SHA1 | Date | |
|---|---|---|---|
|
771b8e5ce4
|
|||
|
b15b8b2ae1
|
|||
|
a0931eb420
|
@@ -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
|
||||||
@@ -201,12 +202,24 @@ pub struct Config {
|
|||||||
pub server: ServerConfig,
|
pub server: ServerConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse configuration from the expected files and environment variables
|
impl Config {
|
||||||
pub fn load_config() -> Result<Config, config::ConfigError> {
|
/// Parse configuration from the expected files and environment variables
|
||||||
|
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",
|
||||||
@@ -220,13 +233,8 @@ pub fn load_config() -> Result<Config, config::ConfigError> {
|
|||||||
)?
|
)?
|
||||||
.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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provide a sane default for the public path, using the same sources as Dioxus does internally.
|
/// 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;
|
||||||
use crate::app::LOGO_ICO;
|
use crate::app::LOGO_ICO;
|
||||||
use crate::server::{
|
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,
|
require_auth_mw::require_auth_middleware,
|
||||||
};
|
};
|
||||||
use crate::util::error::{Contextualize, Error, ErrorType, Result};
|
use crate::util::error::{Contextualize, Error, ErrorType, Result};
|
||||||
@@ -21,7 +21,7 @@ pub async fn main() -> Result<std::convert::Infallible> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tracing::debug!("Loading configuration...");
|
tracing::debug!("Loading configuration...");
|
||||||
let config = config::load_config()
|
let config = Config::from_env()
|
||||||
.map_err(|e| Error::message_here(e.to_string()))
|
.map_err(|e| Error::message_here(e.to_string()))
|
||||||
.err_context("Failed to load config")?;
|
.err_context("Failed to load config")?;
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
/// A location in the source code
|
/// A location in the source code
|
||||||
/// A thin wrapper over `std::panic::Location`, which isn't `Serialize` or `Deserialize`
|
/// A thin wrapper over `std::panic::Location`, which isn't `Serialize` or `Deserialize`
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct ErrorLocation {
|
pub struct ErrorLocation {
|
||||||
file: String,
|
file: String,
|
||||||
line: u32,
|
line: u32,
|
||||||
@@ -59,7 +59,7 @@ fn rand_modal_id() -> String {
|
|||||||
format!("err-modal-{random_str}")
|
format!("err-modal-{random_str}")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, thiserror::Error)]
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, thiserror::Error)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
#[source]
|
#[source]
|
||||||
/// The error type and data
|
/// The error type and data
|
||||||
@@ -307,7 +307,7 @@ impl<T, E: Into<Error>> Contextualize<Result<T>> for E {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, thiserror::Error, Deserialize, Serialize)]
|
#[derive(Debug, Clone, PartialEq, thiserror::Error, Deserialize, Serialize)]
|
||||||
pub enum ErrorType {
|
pub enum ErrorType {
|
||||||
#[error("Authentication error: {0}")]
|
#[error("Authentication error: {0}")]
|
||||||
Auth(AuthError),
|
Auth(AuthError),
|
||||||
@@ -365,7 +365,7 @@ impl From<fred::error::Error> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, thiserror::Error, Deserialize, Serialize)]
|
#[derive(Debug, Clone, PartialEq, thiserror::Error, Deserialize, Serialize)]
|
||||||
pub enum AuthError {
|
pub enum AuthError {
|
||||||
#[error("Invalid credentials")]
|
#[error("Invalid credentials")]
|
||||||
InvalidCredentials,
|
InvalidCredentials,
|
||||||
|
|||||||
Reference in New Issue
Block a user