feat: 推理服务根路径返回欢迎导航页(浏览器直接访问 30800 不再 404)

This commit is contained in:
2026-08-05 09:12:33 +08:00
parent 2ae5653a9a
commit b673830859
+49
View File
@@ -120,8 +120,57 @@ class Handler(BaseHTTPRequestHandler):
self._json(200, {"models": [BACKEND.model],
"backend": BACKEND.backend_name})
return
if self.path in ("/", "/index.html"):
self._send_index()
return
self._json(404, {"error": "not found", "path": self.path})
def _send_index(self) -> None:
"""根路径欢迎页:列出可用接口,便于浏览器直接访问。"""
try:
h = BACKEND.health()
health_s = json.dumps(h, ensure_ascii=False)
except Exception as exc: # noqa: BLE001
health_s = json.dumps({"status": "dry-run",
"reason": str(exc)[:80]},
ensure_ascii=False)
html = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>iAOP Inference Service</title>
<style>
body {{ font-family: "Microsoft YaHei", sans-serif; margin: 40px auto;
max-width: 720px; padding: 0 16px; color: #222; }}
h1 {{ color: #0b5; }}
code {{ background: #f4f4f4; padding: 2px 6px; border-radius: 4px; }}
li {{ margin: 8px 0; }}
.health {{ background: #eef; padding: 10px; border-radius: 6px;
word-break: break-all; }}
</style>
</head>
<body>
<h1>iAOP Inference Service</h1>
<p>iAOP-Core 推理服务({BACKEND.backend_name} 后端 · {BACKEND.model})。
当前为 dry-run 占位模式(未接入真实推理硬件/模型服务)。</p>
<h2>可用接口</h2>
<ul>
<li><a href="/health"><code>GET /health</code></a> — 健康巡检</li>
<li><a href="/v1/models"><code>GET /v1/models</code></a> — 模型信息</li>
<li><code>POST /v1/chat/completions</code> — OpenAI 兼容推理
<br>body: <code>{"{model, prompt, max_tokens}"}</code></li>
</ul>
<h2>当前健康状态</h2>
<div class="health">{health_s}</div>
</body>
</html>"""
body = html.encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_POST(self): # noqa: N802 - http.server 命名
if self.path == "/v1/chat/completions":
body = self._read_body()