Add Web 2 workspace and controlled deployment updates
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"""Run the local Web workspace: python -m ranch_web --demo --no-browser."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from contextlib import asynccontextmanager
|
||||
import threading
|
||||
import webbrowser
|
||||
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
import uvicorn
|
||||
|
||||
from . import __version__
|
||||
from .app import create_app
|
||||
from .config import WebConfig
|
||||
|
||||
|
||||
def main(argv=None) -> int:
|
||||
parser = argparse.ArgumentParser(description="牧羊人之心 Web 工作台(Windows / Linux,无需 Qt)")
|
||||
parser.add_argument("--host", help="监听地址,默认 127.0.0.1;远程绑定必须配置管理密码")
|
||||
parser.add_argument("--port", type=int, help="监听端口,默认 8765")
|
||||
parser.add_argument("--data-dir", help="独立 Web 数据目录,默认 local_state/ranch_web")
|
||||
parser.add_argument("--demo", action="store_true", help="添加一个离线演示账号,不访问游戏服务器")
|
||||
parser.add_argument("--no-browser", action="store_true", help="启动后不自动打开浏览器")
|
||||
parser.add_argument("--version", action="version", version=__version__)
|
||||
args = parser.parse_args(argv)
|
||||
overrides = {key: value for key, value in {"host": args.host, "port": args.port, "data_dir": args.data_dir}.items() if value is not None}
|
||||
try:
|
||||
settings = WebConfig.from_env(**overrides)
|
||||
except (ValueError, TypeError):
|
||||
parser.error("Web 设置无效;请检查监听地址、端口及 RANCH_WEB_PASSWORD 等环境变量")
|
||||
app = create_app(config=settings)
|
||||
application_lifespan = app.router.lifespan_context
|
||||
browser_timer = None
|
||||
|
||||
@asynccontextmanager
|
||||
async def cli_lifespan(application):
|
||||
nonlocal browser_timer
|
||||
async with application_lifespan(application):
|
||||
if args.demo:
|
||||
accounts = await run_in_threadpool(application.state.manager.list_accounts)
|
||||
if not any(item.get("demo") for item in accounts):
|
||||
await run_in_threadpool(application.state.manager.create_account, label="离线演示", demo=True)
|
||||
if not args.no_browser:
|
||||
browser_host = settings.host
|
||||
if browser_host == "0.0.0.0":
|
||||
browser_host = "127.0.0.1"
|
||||
elif browser_host == "::":
|
||||
browser_host = "::1"
|
||||
authority = f"[{browser_host}]" if ":" in browser_host else browser_host
|
||||
browser_timer = threading.Timer(1, webbrowser.open, args=(f"http://{authority}:{settings.port}/",))
|
||||
browser_timer.daemon = True
|
||||
browser_timer.start()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
if browser_timer is not None:
|
||||
browser_timer.cancel()
|
||||
|
||||
app.router.lifespan_context = cli_lifespan
|
||||
# Request bodies, credentials and arbitrary query strings do not belong in access logs.
|
||||
uvicorn.run(app, host=settings.host, port=settings.port, access_log=False, proxy_headers=False)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user