driver / server /config.py
bevisssssss's picture
fix: recover from expired admin token instead of showing a load error
79dca2e
Raw History Blame Contribute Delete
2.84 kB
"""Application configuration — loaded from .env / environment."""
import os
import secrets
from dotenv import load_dotenv
load_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), ".env"))
class Settings:
# MEGA.nz
MEGA_EMAIL: str = os.getenv("MEGA_EMAIL", "")
MEGA_PASSWORD: str = os.getenv("MEGA_PASSWORD", "")
# Proxy (VPN)
HTTPS_PROXY: str | None = os.getenv("HTTPS_PROXY")
HTTP_PROXY: str | None = os.getenv("HTTP_PROXY")
# MES API
MES_API_URL: str = os.getenv(
"MES_API_URL",
"https://vpn.emdoor.com:8099/EIS/MES/helper/dllMain",
)
MES_ACCESS_KEY: str = os.getenv("MES_ACCESS_KEY", "")
MES_ACCESS_SECRET: str = os.getenv("MES_ACCESS_SECRET", "")
# MES proxy relay (HF Spaces → local mes_proxy.py tunnel). Empty = direct MES.
MES_PROXY_URL: str = os.getenv("MES_PROXY_URL", "")
MES_PROXY_TOKEN: str = os.getenv("MES_PROXY_TOKEN", "")
# 后台登录的第三方因素:除了用户名密码,还必须提供此密钥。
# 后台是公网可访问的,默认密码一旦漏出,任何人都能给自己发一个全字段访问码。
# 留空时禁止登录(而非静默放行),避免误配置后以为有这个保护。
ADMIN_ACCESS_KEY: str = os.getenv("ADMIN_ACCESS_KEY", "")
# MES public-query access control
MES_ACCESS_CODE: str = os.getenv("MES_ACCESS_CODE", "mes-2026")
# 未配置时生成随机密钥:绝不能用公开的默认值,否则任何人都能伪造令牌。
# 代价是未配置时重启会令旧令牌失效,生产环境请在环境变量中固定。
SECRET_KEY: str = os.getenv("SECRET_KEY") or secrets.token_urlsafe(48)
MES_TOKEN_TTL: int = int(os.getenv("MES_TOKEN_TTL", "2592000")) # 30 days
MES_CACHE_TTL: int = int(os.getenv("MES_CACHE_TTL", "43200")) # 12h
# Server
HOST: str = os.getenv("HOST", "0.0.0.0")
PORT: int = int(os.getenv("PORT", "8000"))
DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"
# MEGA cache refresh schedule (interval in hours, fractional allowed)
MEGA_CACHE_REFRESH_INTERVAL: float = float(
os.getenv("MEGA_CACHE_REFRESH_INTERVAL", "0.5")
)
# Minimum seconds between two full node-list fetches. Guards the MEGA
# account against burst traffic from concurrent searches.
MEGA_MIN_REFRESH_INTERVAL: int = int(
os.getenv("MEGA_MIN_REFRESH_INTERVAL", "300")
)
# Base backoff (seconds) after a failed fetch, doubling per consecutive
# failure, capped at 2 hours.
MEGA_REFRESH_FAIL_BACKOFF: int = int(
os.getenv("MEGA_REFRESH_FAIL_BACKOFF", "300")
)
# Database
DB_DIR: str = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "data"
)
DB_PATH: str = os.path.join(DB_DIR, "drivers.db")
settings = Settings()