fix: config override

This commit is contained in:
DarkSky
2026-06-30 04:37:52 +08:00
parent a1363b3873
commit a821f67fc9

View File

@@ -6,7 +6,7 @@ use std::{
use napi::Result; use napi::Result;
use serde::Deserialize; use serde::Deserialize;
use serde_json::Map; use serde_json::{Map, Value};
use sqlx::{PgPool, Row}; use sqlx::{PgPool, Row};
use super::{ use super::{
@@ -26,7 +26,7 @@ impl RuntimeConfig {
let database_url = database_url_from_env() let database_url = database_url_from_env()
.or(app_config.database_url()) .or(app_config.database_url())
.unwrap_or_else(|| "postgresql://localhost:5432/affine".to_string()); .unwrap_or_else(|| "postgresql://localhost:5432/affine".to_string());
let storage = ObjectStorageConfig::from_provider_config(app_config.blob_storage_provider_config())?; let storage = ObjectStorageConfig::from_provider_config(app_config.blob_storage_provider_config()?)?;
Ok(Self { database_url, storage }) Ok(Self { database_url, storage })
} }
@@ -37,7 +37,7 @@ impl RuntimeConfig {
// The DB override is loaded after this connection already exists, so it // The DB override is loaded after this connection already exists, so it
// must not rewrite the active datasource URL. // must not rewrite the active datasource URL.
database_url: self.database_url.clone(), database_url: self.database_url.clone(),
storage: ObjectStorageConfig::from_provider_config(app_config.blob_storage_provider_config())?, storage: ObjectStorageConfig::from_provider_config(app_config.blob_storage_provider_config()?)?,
}) })
} }
} }
@@ -46,7 +46,7 @@ impl RuntimeConfig {
struct AppConfigFile { struct AppConfigFile {
db: Option<DbConfigFile>, db: Option<DbConfigFile>,
#[serde(default)] #[serde(default)]
storages: Option<HashMap<String, StorageProviderConfig>>, storages: Option<HashMap<String, Value>>,
} }
#[derive(Debug, Default, Deserialize)] #[derive(Debug, Default, Deserialize)]
@@ -64,11 +64,14 @@ impl AppConfigFile {
.and_then(non_empty_string) .and_then(non_empty_string)
} }
fn blob_storage_provider_config(&self) -> Option<StorageProviderConfig> { fn blob_storage_provider_config(&self) -> Result<Option<StorageProviderConfig>> {
self self
.storages .storages
.as_ref() .as_ref()
.and_then(|storages| storages.get("blob.storage").cloned()) .and_then(|storages| storages.get("blob.storage").cloned())
.map(serde_json::from_value)
.transpose()
.map_err(|err| napi_error(format!("invalid blob storage config: {err}")))
} }
} }
@@ -230,9 +233,13 @@ mod tests {
} }
}), }),
), ),
(
"storages.avatar.publicPath",
serde_json::json!("https://avatar.affineassets.com/"),
),
]) ])
.unwrap(); .unwrap();
let storage = app_config.blob_storage_provider_config().unwrap(); let storage = app_config.blob_storage_provider_config().unwrap().unwrap();
let config = ObjectStorageConfig::from_provider_config(Some(storage)) let config = ObjectStorageConfig::from_provider_config(Some(storage))
.unwrap() .unwrap()
.unwrap(); .unwrap();