Dish_Recognition/dish_server/main.py

54 lines
1.6 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.

# coding=utf-8
"""一键启动总服务器中心 API。
"""
from __future__ import annotations
import os
import sys
_HERE = os.path.dirname(os.path.abspath(__file__))
_ROOT = os.path.dirname(_HERE)
for p in (_HERE, _ROOT):
if p not in sys.path:
sys.path.insert(0, p)
def load_env(path, override=True):
"""加载 .env;默认覆盖已有环境变量,确保云机配置生效。"""
if not os.path.isfile(path):
print(f"[WARN] .env not found: {path}")
return
with open(path, "r", encoding="utf-8-sig") as fh:
for raw in fh:
line = raw.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
key = key.strip()
value = value.strip().strip('"').strip("'")
if not key:
continue
if override or key not in os.environ:
os.environ[key] = value
print(f"[OK] loaded env {path}")
print(f" DISH_MEDIA_ROOT={os.environ.get('DISH_MEDIA_ROOT')}")
print(f" DISH_MEDIA_PUBLIC_BASE={os.environ.get('DISH_MEDIA_PUBLIC_BASE')}")
def main():
load_env(os.path.join(_HERE, ".env"), override=True)
# Linux 云机强制默认(即便没写 .env)
if os.name != "nt":
os.environ.setdefault("DISH_MEDIA_ROOT", "/data/tpweb")
os.environ.setdefault("DISH_MEDIA_PUBLIC_BASE", "https://cpsb.ruimeida.cc/tpweb")
os.chdir(_HERE)
try:
from api_server import serve
except ImportError:
from dish_server.api_server import serve
serve()
if __name__ == "__main__":
main()