49 lines
1.9 KiB
Python
49 lines
1.9 KiB
Python
"""JS → WASM 预编译路由。
|
|||
|
|
|
||
|
|
接口(统一响应格式 {success, data, message}):
|
||
|
|
- POST /api/project/{id}/precompile 把项目 JS 预编译为 WASM(仅博主)
|
||
|
|
- GET /api/project/{id}/wasm-report 查看最近一次编译报告(公开,仅元信息)
|
||
|
|
"""
|
||
|
|
|
||
|
|
import json
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||
|
|
|
||
|
|
from ..auth import get_current_user
|
||
|
|
from ..models import ROLE_BLOGGER, User
|
||
|
|
from ..schemas import UnifiedResponse
|
||
|
|
from ..services import wasm_builder
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api/project", tags=["project-wasm"])
|
||
|
|
|
||
|
|
|
||
|
|
def _require_blogger(user: User) -> None:
|
||
|
|
"""校验当前用户是否为博主(与 routers/project.py 保持一致)。"""
|
||
|
|
if user.role != ROLE_BLOGGER:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_403_FORBIDDEN, detail="只有博主可以管理项目",
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/{project_id}/precompile", response_model=UnifiedResponse)
|
||
|
|
def precompile_project(
|
||
|
|
project_id: int,
|
||
|
|
current_user: User = Depends(get_current_user),
|
||
|
|
) -> UnifiedResponse:
|
||
|
|
"""把项目演示目录里的 .js 文件预编译为 WASM(仅博主,不执行上传代码)。"""
|
||
|
|
_require_blogger(current_user)
|
||
|
|
report = wasm_builder.compile_project(project_id)
|
||
|
|
return UnifiedResponse(success=True, data=report, message="JS 预编译完成")
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/{project_id}/wasm-report", response_model=UnifiedResponse)
|
||
|
|
def get_wasm_report(project_id: int) -> UnifiedResponse:
|
||
|
|
"""查看项目最近一次 WASM 编译报告(公开:仅文件名与大小等元信息)。"""
|
||
|
|
report_path = wasm_builder.DEMOS_DIR / str(project_id) / "wasm" / "report.json"
|
||
|
|
if not report_path.is_file():
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="该项目尚未执行 WASM 预编译",
|
||
|
|
)
|
||
|
|
report = json.loads(report_path.read_text(encoding="utf-8"))
|
||
|
|
return UnifiedResponse(success=True, data=report, message="")
|