Files
mnote/services/ingest_service/app/core/config.py
T

64 lines
1.8 KiB
Python
Raw Normal View History

2026-01-10 10:35:21 +08:00
from __future__ import annotations
2025-11-23 10:55:04 +08:00
from functools import lru_cache
2026-01-10 10:35:21 +08:00
from pathlib import Path
from typing import Tuple, Type
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
2026-01-10 10:35:21 +08:00
from pydantic_settings.sources import (
DotEnvSettingsSource,
EnvSettingsSource,
PydanticBaseSettingsSource,
)
def _repo_root() -> Path:
# services/ingest_service/app/core/config.py -> .../MNOTE
return Path(__file__).resolve().parents[4]
2025-11-23 10:55:04 +08:00
class Settings(BaseSettings):
2026-01-10 10:35:21 +08:00
"""Ingest Service 配置(优先读取仓库根目录的 .env.local/.env)。"""
2025-11-23 10:55:04 +08:00
2026-01-10 10:35:21 +08:00
model_config = SettingsConfigDict(
env_file=(
str(_repo_root() / ".env.local"),
str(_repo_root() / ".env"),
),
env_file_encoding="utf-8",
case_sensitive=False,
extra="ignore",
)
# 服务基础
2025-11-23 10:55:04 +08:00
app_name: str = Field("AIMNOTE Ingest Service", env="INGEST_APP_NAME")
environment: str = Field("development", env="INGEST_ENV")
api_prefix: str = "/api"
2026-01-10 10:35:21 +08:00
# LightRAG(独立服务 7777
2025-11-23 10:55:04 +08:00
lightrag_url: str = Field("http://127.0.0.1:7777", env="LIGHTRAG_URL")
lightrag_api_key: str = Field("", env="LIGHTRAG_API_KEY")
2026-01-10 10:35:21 +08:00
@classmethod
def settings_customise_sources(
cls,
settings_cls: Type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: EnvSettingsSource,
dotenv_settings: DotEnvSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:
# 关键点:优先使用 .env.local/.env 覆盖系统环境变量,避免被机器上的全局 env 污染。
return (
dotenv_settings,
env_settings,
init_settings,
file_secret_settings,
)
2025-11-23 10:55:04 +08:00
@lru_cache()
def get_settings() -> Settings:
return Settings()