110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
"""
|
||
Application configuration
|
||
"""
|
||
from typing import Union, Any
|
||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||
from pydantic import Field, field_validator, ValidationInfo
|
||
|
||
|
||
class Settings(BaseSettings):
|
||
"""Application settings"""
|
||
|
||
model_config = SettingsConfigDict(
|
||
env_file=".env",
|
||
# Don't try to parse json for environment variables
|
||
env_parse_none_str=None,
|
||
)
|
||
|
||
# Application
|
||
app_name: str = "COSMO - Deep Space Explorer"
|
||
api_prefix: str = "/api"
|
||
|
||
# CORS settings - stored as string in env, converted to list
|
||
cors_origins: Union[str, list[str]] = "*"
|
||
|
||
@field_validator('cors_origins', mode='before')
|
||
@classmethod
|
||
def validate_cors_origins(cls, v: Any) -> list[str]:
|
||
"""Parse CORS origins from comma-separated string or JSON array"""
|
||
if v is None:
|
||
return ["*"]
|
||
if isinstance(v, str):
|
||
# Parse comma-separated string
|
||
origins = [origin.strip() for origin in v.split(',') if origin.strip()]
|
||
return origins if origins else ["*"]
|
||
if isinstance(v, list):
|
||
return v
|
||
return ["*"]
|
||
|
||
# Cache settings
|
||
cache_ttl_days: int = 3
|
||
|
||
# JWT settings
|
||
jwt_secret_key: str = "your-secret-key-change-this-in-production"
|
||
jwt_algorithm: str = "HS256"
|
||
jwt_access_token_expire_minutes: int = 60 * 24 # 24 hours
|
||
|
||
# Database settings (PostgreSQL)
|
||
database_host: str = "localhost"
|
||
database_port: int = 5432
|
||
database_name: str = "cosmo_db"
|
||
database_user: str = "postgres"
|
||
database_password: str = "postgres"
|
||
database_pool_size: int = 20
|
||
database_max_overflow: int = 10
|
||
|
||
# Redis settings
|
||
redis_host: str = "localhost"
|
||
redis_port: int = 6379
|
||
redis_db: int = 0
|
||
redis_password: str = ""
|
||
redis_max_connections: int = 50
|
||
|
||
# Terminal (ESP32) device pairing settings
|
||
terminal_pair_code_ttl_seconds: int = 60 * 10 # pairing code validity
|
||
terminal_token_expire_days: int = 365 # long lived terminal token lifetime
|
||
|
||
# 火箭发射事件数据源:The Space Devs Launch Library 2
|
||
launch_library_api_base: str = "https://ll.thespacedevs.com/2.2.0"
|
||
launch_library_token: str = "" # 可选,注册后速率限制更宽松
|
||
launch_library_timeout: int = 30
|
||
|
||
# File upload settings
|
||
upload_dir: str = "upload"
|
||
max_upload_size: int = 10485760 # 10MB
|
||
|
||
# Proxy settings (for accessing NASA JPL Horizons API in China)
|
||
http_proxy: str = ""
|
||
https_proxy: str = ""
|
||
nasa_api_timeout: int = 30
|
||
|
||
@property
|
||
def proxy_dict(self) -> dict[str, str] | None:
|
||
"""Get proxy configuration as a dictionary for httpx"""
|
||
if self.http_proxy or self.https_proxy:
|
||
proxies = {}
|
||
if self.http_proxy:
|
||
proxies["http://"] = self.http_proxy
|
||
if self.https_proxy:
|
||
proxies["https://"] = self.https_proxy
|
||
return proxies
|
||
return None
|
||
|
||
@property
|
||
def database_url(self) -> str:
|
||
"""Construct database URL for SQLAlchemy"""
|
||
return (
|
||
f"postgresql+asyncpg://{self.database_user}:{self.database_password}"
|
||
f"@{self.database_host}:{self.database_port}/{self.database_name}"
|
||
)
|
||
|
||
@property
|
||
def redis_url(self) -> str:
|
||
"""Construct Redis URL"""
|
||
if self.redis_password:
|
||
return f"redis://:{self.redis_password}@{self.redis_host}:{self.redis_port}/{self.redis_db}"
|
||
return f"redis://{self.redis_host}:{self.redis_port}/{self.redis_db}"
|
||
|
||
|
||
settings = Settings()
|