fix(server): 挂载 StaticFiles 统一提供 chat.css / chat_state.js / chat_ws.js
背景:上一轮把 chat.html 的内联 CSS 拆到 chat.css,并新增 <link href="/chat.css">
引用,但 app.py 只为 chat_state.js / chat_ws.js 注册了 FileResponse 端点,
/chat.css 没有对应路由,浏览器加载返回 404,整个页面无样式。
修复:
- app.py 删除 chat_state.js / chat_ws.js 显式端点
- 改用 app.mount('/static', StaticFiles(directory=static_dir))
- chat.html 三处 URL 改为 /static/chat.{css,state.js,ws.js}
优点:未来 chat.js 拆分无需再改 app.py。
验证(serve --fake @ http://127.0.0.1:8000):
- GET /static/chat.css -> 200 text/css; charset=utf-8
- GET /static/chat_state.js -> 200 text/javascript; charset=utf-8
- GET /static/chat_ws.js -> 200 text/javascript; charset=utf-8
- GET /chat.css(旧路径) -> 404(已废弃,HTML 中无引用)
This commit is contained in:
@@ -17,6 +17,7 @@ from typing import Any
|
|||||||
|
|
||||||
from fastapi import FastAPI, File, Form, HTTPException, UploadFile, WebSocket, WebSocketDisconnect
|
from fastapi import FastAPI, File, Form, HTTPException, UploadFile, WebSocket, WebSocketDisconnect
|
||||||
from fastapi.responses import FileResponse, HTMLResponse
|
from fastapi.responses import FileResponse, HTMLResponse
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from genesis.inference.factory import build_inference_engine
|
from genesis.inference.factory import build_inference_engine
|
||||||
@@ -125,19 +126,9 @@ def create_app(
|
|||||||
def index():
|
def index():
|
||||||
return chat_html
|
return chat_html
|
||||||
|
|
||||||
@app.get("/chat_state.js")
|
# 静态资源(chat.css / chat_state.js / chat_ws.js / 未来的 chat.js)
|
||||||
def chat_state_js():
|
# 统一通过 /static/<file> 访问,避免逐个文件注册
|
||||||
p = static_dir / "chat_state.js"
|
app.mount("/static", StaticFiles(directory=str(static_dir), html=False), name="static")
|
||||||
if not p.exists():
|
|
||||||
raise _error(404, "NOT_FOUND", "chat_state.js 不存在")
|
|
||||||
return FileResponse(p, media_type="application/javascript")
|
|
||||||
|
|
||||||
@app.get("/chat_ws.js")
|
|
||||||
def chat_ws_js():
|
|
||||||
p = static_dir / "chat_ws.js"
|
|
||||||
if not p.exists():
|
|
||||||
raise _error(404, "NOT_FOUND", "chat_ws.js 不存在")
|
|
||||||
return FileResponse(p, media_type="application/javascript")
|
|
||||||
|
|
||||||
# ---------- 项目配置 ----------
|
# ---------- 项目配置 ----------
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<title>Genesis — 概要设计书自动生成(对话)</title>
|
<title>Genesis — 概要设计书自动生成(对话)</title>
|
||||||
<link rel="stylesheet" href="/chat.css">
|
<link rel="stylesheet" href="/static/chat.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="sidebar">
|
<div id="sidebar">
|
||||||
@@ -119,7 +119,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/chat_state.js"></script>
|
<script src="/static/chat_state.js"></script>
|
||||||
<script src="/chat_ws.js"></script>
|
<script src="/static/chat_ws.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user