Import source: webfs
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The built frontend is compiled into the binary, so deployment is one file.
|
||||
//
|
||||
//go:embed all:web/dist
|
||||
var webFS embed.FS
|
||||
|
||||
func webHandler() http.Handler {
|
||||
dist, err := fs.Sub(webFS, "web/dist")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fileServer := http.FileServer(http.FS(dist))
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p := strings.TrimPrefix(path.Clean(r.URL.Path), "/")
|
||||
if p != "" {
|
||||
if _, err := fs.Stat(dist, p); err != nil {
|
||||
// SPA fallback: unknown paths serve the app shell.
|
||||
r.URL.Path = "/"
|
||||
}
|
||||
}
|
||||
fileServer.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user