CatPtain commited on
Commit
b4669ea
·
verified ·
1 Parent(s): ae6b1a6

Upload config.py

Browse files
Files changed (1) hide show
  1. app/config/config.py +28 -10
app/config/config.py CHANGED
@@ -6,9 +6,10 @@ import datetime
6
  import json
7
  from typing import Any, Dict, List, Type
8
 
9
- from pydantic import ValidationError, ValidationInfo, field_validator
10
  from pydantic_settings import BaseSettings
11
  from sqlalchemy import insert, select, update
 
12
 
13
  from app.core.constants import (
14
  API_VERSION,
@@ -27,6 +28,29 @@ from app.core.constants import (
27
  from app.log.logger import Logger
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  class Settings(BaseSettings):
31
  # 数据库配置
32
  DATABASE_TYPE: str = "sqlite" # sqlite 或 mysql
@@ -51,16 +75,10 @@ class Settings(BaseSettings):
51
  return v
52
 
53
  # API相关配置
54
- API_KEYS: List[str]
55
- ALLOWED_TOKENS: List[str] = []
56
 
57
- @field_validator("ALLOWED_TOKENS", mode="before")
58
- @classmethod
59
- def parse_allowed_tokens(cls, v: Any) -> List[str]:
60
- if not isinstance(v, str):
61
- v = str(v)
62
- return [token.strip() for token in v.split(",") if token.strip()]
63
- return v
64
  BASE_URL: str = f"https://generativelanguage.googleapis.com/{API_VERSION}"
65
  AUTH_TOKEN: str = ""
66
  MAX_FAILURES: int = 3
 
6
  import json
7
  from typing import Any, Dict, List, Type
8
 
9
+ from pydantic import ValidationError, ValidationInfo, field_validator, GetCoreSchemaHandler
10
  from pydantic_settings import BaseSettings
11
  from sqlalchemy import insert, select, update
12
+ from pydantic_core import PydanticCustomError, core_schema
13
 
14
  from app.core.constants import (
15
  API_VERSION,
 
28
  from app.log.logger import Logger
29
 
30
 
31
+ class CommaSeparatedList(list):
32
+ @classmethod
33
+ def __get_pydantic_core_schema__(
34
+ cls, source_type: Any, handler: GetCoreSchemaHandler
35
+ ) -> core_schema.CoreSchema:
36
+ def validate_from_string(value: str) -> list[str]:
37
+ if not isinstance(value, str):
38
+ raise PydanticCustomError(
39
+ 'comma_separated_list_type',
40
+ 'Value must be a string to be parsed as comma-separated list'
41
+ )
42
+ return [item.strip() for item in value.split(',') if item.strip()]
43
+
44
+ return core_schema.no_info_after_validator_function(
45
+ validate_from_string,
46
+ core_schema.union_schema([
47
+ core_schema.list_schema(core_schema.str_schema()), # Allows direct list of strings
48
+ core_schema.str_schema(), # Allows string input
49
+ ]),
50
+ serialization=core_schema.to_list_ser_schema(core_schema.str_schema())
51
+ )
52
+
53
+
54
  class Settings(BaseSettings):
55
  # 数据库配置
56
  DATABASE_TYPE: str = "sqlite" # sqlite 或 mysql
 
75
  return v
76
 
77
  # API相关配置
78
+ API_KEYS: CommaSeparatedList # Changed to custom type
79
+ ALLOWED_TOKENS: CommaSeparatedList = CommaSeparatedList() # Changed to custom type and default
80
 
81
+ # Removed the old field_validator for ALLOWED_TOKENS
 
 
 
 
 
 
82
  BASE_URL: str = f"https://generativelanguage.googleapis.com/{API_VERSION}"
83
  AUTH_TOKEN: str = ""
84
  MAX_FAILURES: int = 3