task: add real estate story
All checks were successful
Gitea Actions Demo / deploy (push) Successful in 26s
All checks were successful
Gitea Actions Demo / deploy (push) Successful in 26s
This commit is contained in:
1
config/__init__.py
Normal file
1
config/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from config.settings import settings
|
||||
18
config/database.py
Normal file
18
config/database.py
Normal file
@ -0,0 +1,18 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, scoped_session
|
||||
from config.settings import settings
|
||||
|
||||
SQLALCHEMY_SYNC_URL = (
|
||||
f"postgresql+psycopg://{settings.DB_USER}:{settings.DB_PASS}"
|
||||
f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}"
|
||||
)
|
||||
|
||||
engine = create_engine(
|
||||
SQLALCHEMY_SYNC_URL,
|
||||
echo=False, # 开发可改 True
|
||||
future=True
|
||||
)
|
||||
|
||||
SessionLocal = scoped_session(
|
||||
sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
)
|
||||
22
config/env_loader.py
Normal file
22
config/env_loader.py
Normal file
@ -0,0 +1,22 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
def load_env():
|
||||
"""
|
||||
自动根据 ENV 加载对应的 .env 文件
|
||||
"""
|
||||
base_file = ".env"
|
||||
prod_file = ".env.prod"
|
||||
test_file = ".env.test"
|
||||
|
||||
# 先加载基础 .env
|
||||
if os.path.exists(base_file):
|
||||
load_dotenv(base_file)
|
||||
|
||||
# 根据参数 ENV 再加载其他环境
|
||||
env = os.getenv("ENV", "dev")
|
||||
|
||||
if env == "prod" and os.path.exists(prod_file):
|
||||
load_dotenv(prod_file, override=True)
|
||||
elif env == "test" and os.path.exists(test_file):
|
||||
load_dotenv(test_file, override=True)
|
||||
34
config/settings.py
Normal file
34
config/settings.py
Normal file
@ -0,0 +1,34 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic import Field
|
||||
from config.env_loader import load_env
|
||||
|
||||
# 先加载 ENV & .env
|
||||
load_env()
|
||||
|
||||
class Settings(BaseSettings):
|
||||
# 环境
|
||||
ENV: str = Field("dev")
|
||||
DEBUG: bool = Field(True)
|
||||
|
||||
# 日志
|
||||
LOG_LEVEL: str = Field("LOG_LEVEL")
|
||||
LOG_FILE_PATH: str = Field("logs")
|
||||
LOG_TYPE: str = Field("console")
|
||||
|
||||
# 数据库
|
||||
DB_HOST: str
|
||||
DB_PORT: int
|
||||
DB_USER: str
|
||||
DB_PASS: str
|
||||
DB_NAME: str
|
||||
|
||||
# LLM配置
|
||||
LLM_API_KEY: str = Field("LLM_API_KEY")
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
env_file_encoding = "utf-8"
|
||||
|
||||
|
||||
# 全局唯一配置实例
|
||||
settings = Settings()
|
||||
Reference in New Issue
Block a user