我有一个VueJS用于提供 Web 界面的 go 项目。在构建项目时,我首先使用npm run build编译前端代码,该代码是在gui/dist我的项目目录下生成的。然后我使用此代码来提供静态内容:
//go:embed gui/dist/*
var dist embed.FS
gui := http.FileServer(http.FS(dist))
http.Handle("/", gui)
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
msg := fmt.Sprintf("TODO: %s", r.URL)
http.Error(w, msg, http.StatusNotImplemented)
})
svr := http.Server{
Addr: fmt.Sprintf(":%v", cf.HTTPPort),
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
}
assert(svr.ListenAndServe())
Run Code Online (Sandbox Code Playgroud)
问题是,在浏览器中打开站点时,它显示了一个浏览文件界面,即以gui开头,然后进入dist,然后显示该index.html文件,该文件是一个空白页面,因为它需要诸如 之类的文件/css/...,而go Web 服务器在/gui/dist/css/....
我尝试使用http.StripPrefix()但显然它不是为了处理这种情况,或者我没有正确使用它:
http.Handle("/", http.StripPrefix("/gui/dist", gui))
Run Code Online (Sandbox Code Playgroud)
这生成了一个404 page not found.
was*_*mup 11
你可以使用fs.Sub(dist, "gui/dist"),试试看:
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
)
func main() {
http.Handle("/", http.FileServer(getFileSystem()))
http.HandleFunc("/api/", api)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
func getFileSystem() http.FileSystem {
fsys, err := fs.Sub(dist, "gui/dist")
if err != nil {
log.Fatal(err)
}
return http.FS(fsys)
}
func api(w http.ResponseWriter, r *http.Request) {
msg := fmt.Sprintf("TODO: %s", r.URL)
http.Error(w, msg, http.StatusNotImplemented)
}
//go:embed gui/dist/*
var dist embed.FS
Run Code Online (Sandbox Code Playgroud)