feat: 服务管理支持 HTTP/HTTPS 协议选择

- Service 增加 scheme 字段,build_url 按协议拼接地址
- 表单新增协议下拉;启动时自动迁移已有 SQLite 库
- 更新部署说明中的 HTTPS 服务添加示例

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-05-30 11:37:32 +08:00
parent a8cf3422e4
commit fd5e333daf
5 changed files with 62 additions and 5 deletions
+25
View File
@@ -118,6 +118,7 @@ def create_app() -> Flask:
with app.app_context():
db.create_all()
_migrate_schema()
_ensure_default_user()
_ensure_admin_from_env()
@@ -254,6 +255,7 @@ def create_app() -> Flask:
path = (form.path.data or "").strip() or "/"
s = Service(
name=form.name.data.strip(),
scheme=form.scheme.data,
host=form.host.data.strip(),
port=form.port.data,
path=path,
@@ -288,6 +290,7 @@ def create_app() -> Flask:
).all()
if form.validate_on_submit():
s.name = form.name.data.strip()
s.scheme = form.scheme.data
s.host = form.host.data.strip()
s.port = form.port.data
s.path = (form.path.data or "").strip() or "/"
@@ -331,6 +334,28 @@ def create_app() -> Flask:
return app
def _migrate_schema() -> None:
"""SQLite 已有库补列(db.create_all 不会 ALTER 已有表)。"""
from sqlalchemy import inspect, text
try:
insp = inspect(db.engine)
if "services" not in insp.get_table_names():
return
cols = {c["name"] for c in insp.get_columns("services")}
if "scheme" not in cols:
with db.engine.begin() as conn:
conn.execute(
text(
"ALTER TABLE services ADD COLUMN scheme VARCHAR(8) "
"NOT NULL DEFAULT 'http'"
)
)
print("[nav] 已为 services 表添加 scheme 列(默认 http)。", flush=True)
except Exception as exc:
print(f"[nav] 数据库结构迁移跳过或失败: {exc}", flush=True)
def _first_group_id() -> Optional[int]:
g = ServiceGroup.query.order_by(ServiceGroup.sort_order, ServiceGroup.id).first()
return g.id if g else None