ar07xd commited on
Commit
7b6ec69
·
verified ·
1 Parent(s): 7bf8093

Sync from GitHub via hub-sync

Browse files
Files changed (1) hide show
  1. config.py +24 -7
config.py CHANGED
@@ -1,7 +1,7 @@
1
  import json
2
- from typing import Any
3
  from pydantic import field_validator
4
- from pydantic_settings import BaseSettings, SettingsConfigDict
5
 
6
 
7
  class Settings(BaseSettings):
@@ -9,16 +9,33 @@ class Settings(BaseSettings):
9
  APP_HOST: str = "0.0.0.0"
10
  APP_PORT: int = 8000
11
  DEBUG: bool = False
12
- CORS_ORIGINS: list[str] = ["http://localhost:5173"]
 
 
13
 
14
  @field_validator("CORS_ORIGINS", mode="before")
15
  @classmethod
16
  def assemble_cors_origins(cls, v: Any) -> list[str]:
17
  """Parse CORS_ORIGINS from string (JSON or comma-separated) into a list."""
18
- if isinstance(v, str) and not v.startswith("["):
19
- return [i.strip() for i in v.split(",")]
20
- elif isinstance(v, str) and v.startswith("["):
21
- return json.loads(v)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  return v
23
 
24
  # Database
 
1
  import json
2
+ from typing import Annotated, Any
3
  from pydantic import field_validator
4
+ from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
5
 
6
 
7
  class Settings(BaseSettings):
 
9
  APP_HOST: str = "0.0.0.0"
10
  APP_PORT: int = 8000
11
  DEBUG: bool = False
12
+ # NoDecode prevents pydantic-settings from forcing JSON parsing first.
13
+ # This lets us accept JSON arrays, CSV strings, single origins, and empty values.
14
+ CORS_ORIGINS: Annotated[list[str], NoDecode] = ["http://localhost:5173"]
15
 
16
  @field_validator("CORS_ORIGINS", mode="before")
17
  @classmethod
18
  def assemble_cors_origins(cls, v: Any) -> list[str]:
19
  """Parse CORS_ORIGINS from string (JSON or comma-separated) into a list."""
20
+ if v is None:
21
+ return ["http://localhost:5173"]
22
+
23
+ if isinstance(v, str):
24
+ raw = v.strip()
25
+ if not raw:
26
+ return ["http://localhost:5173"]
27
+
28
+ if raw.startswith("["):
29
+ parsed = json.loads(raw)
30
+ if isinstance(parsed, list):
31
+ return [str(i).strip() for i in parsed if str(i).strip()]
32
+
33
+ # Supports single origin and comma-separated origins.
34
+ return [i.strip() for i in raw.split(",") if i.strip()]
35
+
36
+ if isinstance(v, list):
37
+ return [str(i).strip() for i in v if str(i).strip()]
38
+
39
  return v
40
 
41
  # Database