config.py•2.3 kB
"""애플리케이션 설정 관리"""
import os
from typing import Optional
from pydantic import Field
from pydantic_settings import BaseSettings
class DatabaseConfig(BaseSettings):
"""데이터베이스 설정"""
host: str = "localhost"
port: int = 5432
name: str = "market_stats"
user: str = "market_user"
password: str = ""
model_config = {
"env_prefix": "TIMESCALE_DB_",
"env_file": ".env",
"case_sensitive": False
}
@property
def url(self) -> str:
"""데이터베이스 연결 URL 생성"""
return f"postgresql://{self.user}:{self.password}@{self.host}:{self.port}/{self.name}"
class RedisConfig(BaseSettings):
"""Redis 설정"""
host: str = "localhost"
port: int = 6379
db: int = 0
password: Optional[str] = None
model_config = {
"env_prefix": "REDIS_",
"env_file": ".env",
"case_sensitive": False
}
@property
def url(self) -> str:
"""Redis 연결 URL 생성"""
auth = f":{self.password}@" if self.password else ""
return f"redis://{auth}{self.host}:{self.port}/{self.db}"
class APIConfig(BaseSettings):
"""API 설정"""
host: str = "0.0.0.0"
port: int = 8000
debug: bool = False
secret_key: str = "dev-secret"
api_key_header: str = "X-API-Key"
model_config = {
"env_prefix": "API_",
"env_file": ".env",
"case_sensitive": False
}
class LoggingConfig(BaseSettings):
"""로깅 설정"""
level: str = "INFO"
format: str = "json"
model_config = {
"env_prefix": "LOG_",
"env_file": ".env",
"case_sensitive": False
}
class ExternalAPIConfig(BaseSettings):
"""외부 API 설정"""
krx_api_key: Optional[str] = None
bloomberg_api_key: Optional[str] = None
model_config = {
"env_file": ".env",
"case_sensitive": False
}
class AppConfig:
"""전체 애플리케이션 설정"""
def __init__(self):
self.database = DatabaseConfig()
self.redis = RedisConfig()
self.api = APIConfig()
self.logging = LoggingConfig()
self.external_apis = ExternalAPIConfig()
# 글로벌 설정 인스턴스
config = AppConfig()