Files
LLZQ-server/app/config.py
2025-11-19 05:05:37 +08:00

48 lines
1.3 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.
# app/config.py
import os
from functools import lru_cache
class Settings:
"""
后端配置:
- APP_ENV: dev / prod
- DB_*: 数据库连接参数
- 默认库dev 用 LLZQ-testprod 用 LLZQ
"""
def __init__(self) -> None:
self.app_env = os.getenv("APP_ENV", "dev").lower()
self.db_host = os.getenv("DB_HOST", "127.0.0.1")
self.db_port = int(os.getenv("DB_PORT", "5432"))
self.db_user = os.getenv("DB_USER", "local-Python")
self.db_password = os.getenv("DB_PASSWORD", "")
# 默认连接的库,根据 APP_ENV 决定
if self.app_env == "prod":
self.db_name_default = "LLZQ"
else:
self.db_name_default = "LLZQ-test"
self.db_schema = os.getenv("DB_SCHEMA", "XCX")
def resolve_db_name(self, env_header: str | None) -> str:
"""
根据前端传来的 X-LLZQ-Env 决定最终使用哪个 DB
- "prod" => LLZQ
- 其它/为空 => LLZQ-test
"""
if env_header is None:
return self.db_name_default
env_header = env_header.lower()
if env_header == "prod":
return "LLZQ"
else:
return "LLZQ-test"
@lru_cache()
def get_settings() -> Settings:
return Settings()