Compare commits
2 Commits
598215e50e
...
81d8a96d59
| Author | SHA1 | Date | |
|---|---|---|---|
|
81d8a96d59
|
|||
|
f9c6f1afd1
|
@@ -1,8 +1,78 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct DatabaseConfig {
|
||||
#[serde(flatten)]
|
||||
connection: DatabaseConnectionConfig,
|
||||
}
|
||||
|
||||
impl DatabaseConfig {
|
||||
/// Get the configured database connection URI
|
||||
pub fn connection_uri(&self) -> String {
|
||||
self.connection.as_uri()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum DatabaseConnectionConfig {
|
||||
FromUrl {
|
||||
url: String,
|
||||
},
|
||||
FromParts {
|
||||
host: String,
|
||||
port: Option<u16>,
|
||||
database: Option<String>,
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
impl DatabaseConnectionConfig {
|
||||
/// Convert this configuration into the Postgres connection URI
|
||||
pub fn as_uri(&self) -> String {
|
||||
match self {
|
||||
Self::FromUrl { url } => url.clone(),
|
||||
Self::FromParts {
|
||||
host,
|
||||
port,
|
||||
database,
|
||||
username,
|
||||
password,
|
||||
} => {
|
||||
let mut url = "postgres://".to_string();
|
||||
|
||||
if let Some(username) = username {
|
||||
url.push_str(username);
|
||||
|
||||
if let Some(password) = password {
|
||||
url.push_str(&format!(":{password}"));
|
||||
}
|
||||
|
||||
url.push('@');
|
||||
}
|
||||
|
||||
url.push_str(host);
|
||||
|
||||
if let Some(port) = port {
|
||||
url.push_str(&format!(":{port}"));
|
||||
}
|
||||
|
||||
if let Some(database) = database {
|
||||
url.push_str(&format!("/{database}"));
|
||||
}
|
||||
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
/// Top-level application configuration
|
||||
pub struct Config {}
|
||||
pub struct Config {
|
||||
pub database: DatabaseConfig,
|
||||
}
|
||||
|
||||
/// Parse configuration from the expected files and environment variables
|
||||
pub fn load_config() -> Result<Config, config::ConfigError> {
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
use crate::App;
|
||||
use crate::util::error::{Error, Result};
|
||||
use crate::server::config;
|
||||
use crate::util::error::{Contextualize, Error, Result};
|
||||
|
||||
pub fn main() -> Result<()> {
|
||||
if let Err(e) = dotenvy::dotenv() {
|
||||
tracing::warn!("Error reading .env: {e}");
|
||||
}
|
||||
|
||||
tracing::debug!("Loading configuration...");
|
||||
let config = config::load_config()
|
||||
.map_err(|e| Error::message_here(e.to_string()))
|
||||
.err_context("Failed to load config")?;
|
||||
|
||||
tracing::info!("Setup complete, launching web server...");
|
||||
dioxus::launch(App);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user