run_web.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import os
  2. import sys
  3. import webbrowser
  4. from waitress import serve
  5. def _load_dotenv_if_exists():
  6. try:
  7. from dotenv import load_dotenv
  8. except Exception:
  9. return
  10. exe_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
  11. for name in ("config.env",):
  12. env_path = os.path.join(exe_dir, name)
  13. if os.path.exists(env_path):
  14. load_dotenv(env_path, override=True)
  15. break
  16. def main():
  17. _load_dotenv_if_exists()
  18. # 注意:必须在加载 config.env 之后再导入 app.py
  19. # 否则 app.py 顶层读取环境变量(DB/AI 等)时拿不到最新配置
  20. import app as webapp
  21. host = os.getenv("WEB_HOST", "127.0.0.1")
  22. port = int(os.getenv("WEB_PORT", "5000"))
  23. # 打开浏览器(只在本机访问时打开)
  24. if host in ("127.0.0.1", "localhost"):
  25. try:
  26. webbrowser.open(f"http://127.0.0.1:{port}", new=1)
  27. except Exception:
  28. pass
  29. # 正式服务(比 Flask debug 更稳定)
  30. try:
  31. serve(webapp.app, host=host, port=port, threads=8)
  32. except KeyboardInterrupt:
  33. print("\n\n服务已停止")
  34. except OSError as e:
  35. if "Address already in use" in str(e) or "只允许使用一次" in str(e):
  36. print(f"\n\n✗ 端口 {port} 已被占用!")
  37. print(f" 请检查是否有其他程序在使用该端口")
  38. print(f" 或修改 config.env 中的 WEB_PORT 为其他端口(如 8080)")
  39. else:
  40. print(f"\n\n✗ 服务启动失败: {e}")
  41. import traceback
  42. traceback.print_exc()
  43. except Exception as e:
  44. print(f"\n\n✗ 服务启动失败: {e}")
  45. import traceback
  46. traceback.print_exc()
  47. if __name__ == "__main__":
  48. main()