chore(web): directly read frontend/dist folder but not read it from web folder
This commit is contained in:
+20
-3
@@ -1,6 +1,23 @@
|
||||
package web
|
||||
|
||||
import "embed"
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
//go:embed dist/*
|
||||
var StaticFiles embed.FS
|
||||
// StaticFiles is the built frontend, rooted at the directory Vite writes to
|
||||
// (frontend/dist), so paths inside it are relative to that directory, e.g.
|
||||
// "index.html" or "assets/app.js". The path is relative to the working
|
||||
// directory, so the server is expected to run from the repository root.
|
||||
var StaticFiles fs.FS
|
||||
|
||||
func init() {
|
||||
dir := filepath.Join("frontend", "dist")
|
||||
if _, err := os.Stat(dir); err == nil {
|
||||
StaticFiles = os.DirFS(dir)
|
||||
return
|
||||
}
|
||||
|
||||
StaticFiles = os.DirFS(".")
|
||||
}
|
||||
|
||||
+9
-6
@@ -9,12 +9,11 @@ import (
|
||||
|
||||
var staticFS http.FileSystem
|
||||
|
||||
// This init runs after embed.go's, which sets StaticFiles (Go initializes a
|
||||
// package's files in lexical file-name order). StaticFiles is already rooted
|
||||
// at frontend/dist, so it is served as-is — no further fs.Sub is needed.
|
||||
func init() {
|
||||
subFS, err := fs.Sub(StaticFiles, "dist")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
staticFS = http.FS(subFS)
|
||||
staticFS = http.FS(StaticFiles)
|
||||
}
|
||||
|
||||
func ServeStatic(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -55,7 +54,11 @@ func ServeStatic(w http.ResponseWriter, r *http.Request) {
|
||||
func serveIndexHTML(w http.ResponseWriter) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
data, _ := StaticFiles.ReadFile("dist/index.html")
|
||||
data, err := fs.ReadFile(StaticFiles, "index.html")
|
||||
if err != nil {
|
||||
http.Error(w, "index.html not found", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user