修复中控

This commit is contained in:
dekun
2026-05-30 15:40:17 +08:00
parent a67d7aa58b
commit 1993c7b4b1
4 changed files with 174 additions and 0 deletions
+87
View File
@@ -222,6 +222,7 @@ def create_app() -> Flask:
_migrate_schema()
_ensure_default_user()
_ensure_admin_from_env()
_ensure_gate_scout_services()
@app.route("/login", methods=["GET", "POST"])
def login():
@@ -576,6 +577,92 @@ def _ensure_default_user() -> None:
)
def _ensure_gate_scout_services() -> None:
"""gate_scout_order:扫描端 + 执行器(NAV_SEED_GATE_SCOUT=1;云服务器用域名 + https)。"""
flag = (os.environ.get("NAV_SEED_GATE_SCOUT") or "").strip().lower()
if flag not in ("1", "true", "yes", "on"):
return
scheme = (os.environ.get("NAV_GATE_SCOUT_SCHEME") or "http").strip().lower()
if scheme not in ("http", "https"):
scheme = "http"
default_host = (os.environ.get("NAV_GATE_SCOUT_HOST") or "127.0.0.1").strip() or "127.0.0.1"
scout_host = (os.environ.get("NAV_GATE_SCOUT_SCOUT_HOST") or default_host).strip() or default_host
exec_host = (os.environ.get("NAV_GATE_EXECUTOR_HOST") or default_host).strip() or default_host
default_port = "443" if scheme == "https" else "8088"
default_exec_port = "443" if scheme == "https" else "8090"
try:
scout_port = int(os.environ.get("NAV_GATE_SCOUT_PORT") or default_port)
exec_port = int(os.environ.get("NAV_GATE_EXECUTOR_PORT") or default_exec_port)
except ValueError:
scout_port = 443 if scheme == "https" else 8088
exec_port = 443 if scheme == "https" else 8090
scout_path = (os.environ.get("NAV_GATE_SCOUT_PATH") or "/dashboard").strip() or "/dashboard"
exec_path = (os.environ.get("NAV_GATE_EXECUTOR_PATH") or "/dashboard").strip() or "/dashboard"
if not scout_path.startswith("/"):
scout_path = "/" + scout_path
if not exec_path.startswith("/"):
exec_path = "/" + exec_path
update_existing = (os.environ.get("NAV_GATE_SCOUT_UPDATE") or "").strip().lower() in (
"1",
"true",
"yes",
"on",
)
group_name = (os.environ.get("NAV_GATE_SCOUT_GROUP") or "Gate 扫单").strip() or "Gate 扫单"
g = ServiceGroup.query.filter_by(name=group_name).first()
if not g:
g = ServiceGroup(name=group_name, sort_order=50)
db.session.add(g)
db.session.flush()
defs = (
("Gate 扫描端", scout_host, scout_port, scout_path, 0),
("Gate 下单执行器", exec_host, exec_port, exec_path, 10),
)
added = 0
updated = 0
for name, h, port, path, order in defs:
existing = Service.query.filter_by(group_id=g.id, name=name).first()
if existing:
if update_existing and (
existing.scheme != scheme
or existing.host != h
or existing.port != port
or existing.path != path
):
existing.scheme = scheme
existing.host = h
existing.port = port
existing.path = path
updated += 1
continue
db.session.add(
Service(
name=name,
scheme=scheme,
host=h,
port=port,
path=path,
sort_order=order,
group_id=g.id,
embed_kind="",
)
)
added += 1
if added or updated:
db.session.commit()
print(
f"[nav] Gate 扫单:新增 {added}、更新 {updated}(分组「{group_name}」)。"
f" 扫描 {scheme}://{scout_host}:{scout_port}{scout_path}"
f"执行器 {scheme}://{exec_host}:{exec_port}{exec_path}",
flush=True,
)
def _ensure_admin_from_env() -> None:
"""库中已有用户后,.env 里的管理员账号不会自动覆盖旧库;若配置了用户名且尚不存在则补建。"""
username = (os.environ.get("NAV_ADMIN_USERNAME") or "").strip()