refactor: adapters configured from config crate
This commit is contained in:
@@ -5,21 +5,25 @@ use std::sync::Arc;
|
||||
pub struct StorageConfig(Arc<dyn ObjectStore>);
|
||||
|
||||
impl StorageConfig {
|
||||
pub fn from_env() -> anyhow::Result<Self> {
|
||||
let backend = std::env::var("IMAGE_STORAGE_BACKEND").unwrap_or_else(|_| "local".into());
|
||||
|
||||
let store: Arc<dyn ObjectStore> = match backend.as_str() {
|
||||
pub fn from_config(config: &::config::StorageConfig) -> anyhow::Result<Self> {
|
||||
let store: Arc<dyn ObjectStore> = match config.backend.as_str() {
|
||||
"s3" => build_s3_store(
|
||||
&std::env::var("MINIO_ENDPOINT").context("MINIO_ENDPOINT required")?,
|
||||
&std::env::var("MINIO_ACCESS_KEY_ID").context("MINIO_ACCESS_KEY_ID required")?,
|
||||
&std::env::var("MINIO_SECRET_ACCESS_KEY")
|
||||
config
|
||||
.endpoint
|
||||
.as_deref()
|
||||
.context("MINIO_ENDPOINT required")?,
|
||||
config
|
||||
.access_key_id
|
||||
.as_deref()
|
||||
.context("MINIO_ACCESS_KEY_ID required")?,
|
||||
config
|
||||
.secret_access_key
|
||||
.as_deref()
|
||||
.context("MINIO_SECRET_ACCESS_KEY required")?,
|
||||
&std::env::var("MINIO_BUCKET").context("MINIO_BUCKET required")?,
|
||||
&std::env::var("MINIO_REGION").unwrap_or_else(|_| "minio".to_string()),
|
||||
)?,
|
||||
"local" => build_local_store(
|
||||
&std::env::var("IMAGE_STORAGE_PATH").unwrap_or_else(|_| "./images".into()),
|
||||
config.bucket.as_deref().context("MINIO_BUCKET required")?,
|
||||
&config.region,
|
||||
)?,
|
||||
"local" => build_local_store(&config.path)?,
|
||||
other => {
|
||||
anyhow::bail!("Unknown IMAGE_STORAGE_BACKEND: {other:?}. Valid values: s3, local")
|
||||
}
|
||||
|
||||
@@ -127,9 +127,9 @@ impl EventHandler for ImageCleanupHandler {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create() -> anyhow::Result<Arc<dyn ObjectStorage>> {
|
||||
pub fn create(config: &::config::StorageConfig) -> anyhow::Result<Arc<dyn ObjectStorage>> {
|
||||
Ok(Arc::new(ObjectStorageAdapter::from_config(
|
||||
StorageConfig::from_env()?,
|
||||
StorageConfig::from_config(config)?,
|
||||
)))
|
||||
}
|
||||
|
||||
|
||||
@@ -15,3 +15,22 @@ fn local_store_succeeds_if_dir_already_exists() {
|
||||
let result = build_local_store(dir.to_str().unwrap());
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn s3_backend_requires_endpoint() {
|
||||
let section = ::config::StorageConfig {
|
||||
backend: "s3".into(),
|
||||
..Default::default()
|
||||
};
|
||||
let err = StorageConfig::from_config(§ion).err().unwrap();
|
||||
assert!(err.to_string().contains("MINIO_ENDPOINT required"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_backend_is_error() {
|
||||
let section = ::config::StorageConfig {
|
||||
backend: "ftp".into(),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(StorageConfig::from_config(§ion).is_err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user