解决跨越问题
parent
6699538e0a
commit
245616d15b
|
|
@ -1,38 +1,40 @@
|
|||
"""
|
||||
Application configuration
|
||||
"""
|
||||
from typing import Union
|
||||
from typing import Union, Any
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
from pydantic import Field, field_validator, ValidationInfo
|
||||
|
||||
|
||||
def parse_cors_origins(v: Union[str, list]) -> list[str]:
|
||||
"""Parse CORS origins from comma-separated string or list"""
|
||||
if isinstance(v, str):
|
||||
# Parse comma-separated string
|
||||
return [origin.strip() for origin in v.split(',') if origin.strip()]
|
||||
if isinstance(v, list):
|
||||
return v
|
||||
return ["*"]
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Application settings"""
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env")
|
||||
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 - allow all origins for development (IP access support)
|
||||
cors_origins: list[str] = Field(default_factory=lambda: ["*"])
|
||||
# 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: Union[str, list], info: ValidationInfo) -> list[str]:
|
||||
def validate_cors_origins(cls, v: Any) -> list[str]:
|
||||
"""Parse CORS origins from comma-separated string or JSON array"""
|
||||
return parse_cors_origins(v)
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue