2026-01-29 17:02:09 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import ClassVar, Literal
|
2026-02-05 15:13:06 +08:00
|
|
|
from urllib.parse import quote
|
2026-01-29 17:02:09 +08:00
|
|
|
|
|
|
|
|
from pydantic import BaseModel, Field, computed_field
|
|
|
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RuntimeSettings(BaseModel):
|
|
|
|
|
environment: Literal["dev", "test", "prod"] = "dev"
|
|
|
|
|
debug: bool = True
|
|
|
|
|
log_level: str = "INFO"
|
|
|
|
|
log_json: bool = True
|
|
|
|
|
log_rotation: Literal["time", "size", "none"] = "time"
|
|
|
|
|
log_rotation_when: str = "midnight"
|
|
|
|
|
log_rotation_interval: int = 1
|
|
|
|
|
log_rotation_backup_count: int = 14
|
|
|
|
|
log_rotation_max_bytes: int = 10_000_000
|
|
|
|
|
log_dir: str = "logs"
|
|
|
|
|
log_error_dir: str = "logs/errors"
|
|
|
|
|
log_file_name: str = "app.log"
|
|
|
|
|
log_error_file_name: str = "error.log"
|
|
|
|
|
log_sensitive_fields: list[str] = Field(
|
|
|
|
|
default_factory=lambda: [
|
|
|
|
|
"password",
|
|
|
|
|
"secret",
|
|
|
|
|
"token",
|
|
|
|
|
"api_key",
|
|
|
|
|
"authorization",
|
|
|
|
|
"cookie",
|
|
|
|
|
"client_ip",
|
|
|
|
|
"user_id",
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
sql_log_queries: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AppSettings(BaseModel):
|
|
|
|
|
host: str = "0.0.0.0"
|
|
|
|
|
port: int = Field(default=8000, ge=1, le=65535)
|
|
|
|
|
reload: bool = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CorsSettings(BaseModel):
|
|
|
|
|
allow_origins: list[str] = Field(
|
|
|
|
|
default_factory=lambda: [
|
|
|
|
|
"http://localhost",
|
|
|
|
|
"http://localhost:3000",
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
allow_credentials: bool = True
|
|
|
|
|
allow_methods: list[str] = Field(default_factory=lambda: ["*"])
|
|
|
|
|
allow_headers: list[str] = Field(default_factory=lambda: ["*"])
|
|
|
|
|
|
|
|
|
|
|
2026-02-05 15:13:06 +08:00
|
|
|
class RedisSettings(BaseModel):
|
|
|
|
|
host: str = "redis"
|
|
|
|
|
port: int = 6379
|
|
|
|
|
password: str | None = None
|
|
|
|
|
db: int = 0
|
|
|
|
|
socket_connect_timeout: float = 1.0
|
|
|
|
|
socket_timeout: float = 1.0
|
|
|
|
|
max_connections: int = 10
|
|
|
|
|
|
|
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def url(self) -> str:
|
|
|
|
|
if self.password:
|
|
|
|
|
password = quote(self.password, safe="")
|
|
|
|
|
return f"redis://:{password}@{self.host}:{self.port}/{self.db}"
|
|
|
|
|
return f"redis://{self.host}:{self.port}/{self.db}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QdrantSettings(BaseModel):
|
|
|
|
|
host: str = "qdrant"
|
|
|
|
|
port: int = 6333
|
|
|
|
|
grpc_port: int = 6334
|
|
|
|
|
api_key: str | None = None
|
|
|
|
|
https: bool = False
|
|
|
|
|
prefer_grpc: bool = True
|
|
|
|
|
timeout: int = 5
|
|
|
|
|
|
|
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def url(self) -> str:
|
|
|
|
|
scheme = "https" if self.https else "http"
|
|
|
|
|
return f"{scheme}://{self.host}:{self.port}"
|
|
|
|
|
|
|
|
|
|
|
2026-01-29 17:02:09 +08:00
|
|
|
class SupabaseSettings(BaseModel):
|
2026-02-05 15:13:06 +08:00
|
|
|
public_scheme: str = "http"
|
|
|
|
|
public_host: str = "localhost"
|
|
|
|
|
kong_http_port: int = 8000
|
2026-01-29 17:02:09 +08:00
|
|
|
anon_key: str = "CHANGE_ME"
|
|
|
|
|
service_role_key: str = "CHANGE_ME"
|
|
|
|
|
jwt_secret: str | None = None
|
|
|
|
|
|
2026-02-05 15:13:06 +08:00
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def public_url(self) -> str:
|
|
|
|
|
return f"{self.public_scheme}://{self.public_host}:{self.kong_http_port}"
|
2026-01-29 17:02:09 +08:00
|
|
|
|
2026-02-05 15:13:06 +08:00
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def api_external_url(self) -> str:
|
|
|
|
|
return self.public_url
|
2026-01-29 17:02:09 +08:00
|
|
|
|
2026-02-05 15:13:06 +08:00
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def url(self) -> str:
|
|
|
|
|
return self.public_url
|
2026-01-29 17:02:09 +08:00
|
|
|
|
|
|
|
|
|
2026-02-05 15:13:06 +08:00
|
|
|
class DatabaseSettings(BaseModel):
|
|
|
|
|
host: str = "localhost"
|
|
|
|
|
port: int = 5432
|
|
|
|
|
name: str = "postgres"
|
|
|
|
|
user: str = "postgres"
|
|
|
|
|
password: str = "CHANGE_ME"
|
2026-01-29 17:02:09 +08:00
|
|
|
|
2026-02-05 15:13:06 +08:00
|
|
|
@computed_field
|
|
|
|
|
@property
|
|
|
|
|
def url(self) -> str:
|
|
|
|
|
password = quote(self.password, safe="")
|
|
|
|
|
return (
|
|
|
|
|
f"postgresql+asyncpg://{self.user}:{password}"
|
|
|
|
|
f"@{self.host}:{self.port}/{self.name}"
|
|
|
|
|
)
|
2026-01-29 17:02:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _resolve_env_file() -> str:
|
|
|
|
|
current = Path(__file__).resolve()
|
|
|
|
|
for parent in [current, *current.parents]:
|
|
|
|
|
candidate = parent / ".env"
|
|
|
|
|
if candidate.is_file():
|
|
|
|
|
return str(candidate)
|
|
|
|
|
return ".env"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
|
|
|
runtime: RuntimeSettings = RuntimeSettings()
|
|
|
|
|
app: AppSettings = AppSettings()
|
|
|
|
|
cors: CorsSettings = CorsSettings()
|
2026-02-05 15:13:06 +08:00
|
|
|
redis: RedisSettings = RedisSettings()
|
|
|
|
|
qdrant: QdrantSettings = QdrantSettings()
|
|
|
|
|
supabase: SupabaseSettings = SupabaseSettings()
|
|
|
|
|
|
|
|
|
|
database: DatabaseSettings = DatabaseSettings()
|
2026-01-29 17:02:09 +08:00
|
|
|
|
|
|
|
|
@computed_field
|
2026-02-05 15:13:06 +08:00
|
|
|
@property
|
|
|
|
|
def database_url(self) -> str:
|
|
|
|
|
return self.database.url
|
2026-01-29 17:02:09 +08:00
|
|
|
|
|
|
|
|
model_config: ClassVar[SettingsConfigDict] = SettingsConfigDict(
|
|
|
|
|
env_file=_resolve_env_file(),
|
|
|
|
|
env_prefix="SOCIAL_",
|
|
|
|
|
env_nested_delimiter="__",
|
|
|
|
|
case_sensitive=False,
|
|
|
|
|
extra="ignore",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config = Settings()
|