package web import ( "embed" "io" "io/fs" "net/http" "path" "strings" ) // 前端文件嵌入 - 这个路径会在构建脚本中被替换 // //go:embed frontend/* var FrontendFiles embed.FS // hasFrontendFiles 检查是否有前端文件 func hasFrontendFiles() bool { entries, err := FrontendFiles.ReadDir("frontend") if err != nil { return false } return len(entries) > 0 } // CreateFrontendHandler 创建前端文件处理器 func CreateFrontendHandler() http.Handler { if !hasFrontendFiles() { return &placeholderHandler{} } frontendFS, err := fs.Sub(FrontendFiles, "frontend") if err != nil { return &placeholderHandler{} } return &spaHandler{fs: frontendFS} } // placeholderHandler 占位处理器 type placeholderHandler struct{} func (h *placeholderHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) w.Write([]byte(`
# 进入前端目录 cd chuan-next # 安装依赖 yarn install # 构建静态文件 yarn build:ssg # 重新构建 Go 项目以嵌入前端文件 cd .. go build -o file-transfer-server ./cmd
提示: 构建完成后刷新页面即可看到完整的前端界面。