Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions python/aioia_core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,25 @@ def check_api_key(self) -> "HedraSettings":
if self.enabled and not self.api_key:
raise ValueError("API key is required when Hedra is enabled.")
return self


class StorageSettings(BaseSettings):
"""
Storage settings class for configuring storage clients.

Environment variables:
STORAGE_ENABLED: Enable/disable Google Cloud Storage (default: False)
STORAGE_BUCKET_NAME: The name of the Google Cloud Storage bucket to use
STORAGE_PROJECT_ID: Google Cloud project ID (optional)
STORAGE_CREDENTIALS_PATH: Path to the Google Cloud service account JSON key file (optional)
"""

INI_SECTION: ClassVar[str] = "storage"

enabled: bool = False
bucket_name: str = ""
project_id: str | None = None
credentials_path: str | None = None

class Config:
env_prefix = "STORAGE_"
Comment on lines +126 to +145

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

StorageSettings 클래스에 enabledTrue일 때 bucket_name이 설정되었는지 확인하는 유효성 검사기를 추가하는 것이 좋습니다.

현재 enabledTrue이고 bucket_name이 빈 문자열이면, 나중에 GCS 클라이언트를 사용하려고 할 때 런타임 오류가 발생할 수 있습니다.

FishAudioSettingsHedraSettingscheck_api_key처럼 model_validator를 사용하여 설정이 로드될 때 이를 검증하면 더 안전한 코드가 됩니다.

class StorageSettings(BaseSettings):
    """
    Storage settings class for configuring storage clients.

    Environment variables:
        STORAGE_ENABLED: Enable/disable Google Cloud Storage (default: False)
        STORAGE_BUCKET_NAME: The name of the Google Cloud Storage bucket to use
        STORAGE_PROJECT_ID: Google Cloud project ID (optional)
        STORAGE_CREDENTIALS_PATH: Path to the Google Cloud service account JSON key file (optional)
        STORAGE_CREDENTIALS_JSON: JSON string of Google Cloud service account credentials (optional)
    """

    INI_SECTION: ClassVar[str] = "storage"

    enabled: bool = False
    bucket_name: str = ""
    project_id: str | None = None
    credentials_path: str | None = None
    credentials_json: str | None = None

    @model_validator(mode="after")
    def check_bucket_name(self) -> "StorageSettings":
        """Validate that bucket_name is present if the service is enabled."""
        if self.enabled and not self.bucket_name:
            raise ValueError("Bucket name is required when storage is enabled.")
        return self

    class Config:
        env_prefix = "STORAGE_"
References
  1. 개발 가이드의 'Guard Clause' 원칙(11번 라인)에 따라, 서비스 활성화 시 필수 값(bucket_name)을 미리 검증하여 런타임 오류를 방지하는 것이 좋습니다. (link)