Files
Neo-ZQYY/apps/backend/app/config.py

37 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
后端配置加载
优先级(低 → 高):根 .env → 应用 .env.local → 环境变量
敏感值DSN、Token禁止提交仅放在 .env / .env.local 中。
"""
import os
from pathlib import Path
from dotenv import load_dotenv
# 根 .env公共配置
_root_env = Path(__file__).resolve().parents[3] / ".env"
load_dotenv(_root_env, override=False)
# 应用级 .env.local私有覆盖优先级更高
_local_env = Path(__file__).resolve().parents[1] / ".env.local"
load_dotenv(_local_env, override=True)
def get(key: str, default: str | None = None) -> str | None:
"""从环境变量读取配置值。"""
return os.getenv(key, default)
# ---- 数据库连接参数 ----
DB_HOST: str = get("DB_HOST", "localhost")
DB_PORT: str = get("DB_PORT", "5432")
DB_USER: str = get("DB_USER", "")
DB_PASSWORD: str = get("DB_PASSWORD", "")
APP_DB_NAME: str = get("APP_DB_NAME", "zqyy_app")
# ---- 通用 ----
TIMEZONE: str = get("TIMEZONE", "Asia/Shanghai")
LOG_LEVEL: str = get("LOG_LEVEL", "INFO")