package static import ( "embed" "io/fs" "net/http" "sync" ) //go:embed static var staticFS embed.FS var ( indexHTML []byte once sync.Once ) // GetIndex 返回入口页。 // // index.html 缺失时回退到 placeholder.html:前端产物不进版本库, // 新克隆里只有占位页。回退而不是报错是故意的 —— // 只改后端的人应当能直接 go run 起来调 API,而不必先装 node。 func GetIndex() []byte { once.Do(func() { if data, err := fs.ReadFile(staticFS, "static/index.html"); err == nil { indexHTML = data return } indexHTML, _ = fs.ReadFile(staticFS, "static/placeholder.html") }) return indexHTML } func Handler() http.Handler { sub, _ := fs.Sub(staticFS, "static") return http.FileServer(http.FS(sub)) }