From 23fc14cc17409f7726c7187e237479c61a3ff2f8 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Thu, 10 Sep 2026 20:51:11 +0800 Subject: [PATCH] chore(web): directly read `frontend/dist` folder but not read it from `web` folder --- web/embed.go | 23 ++++++++++++++++++++--- web/handler.go | 15 +++++++++------ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/web/embed.go b/web/embed.go index 65206e0..139a5e2 100644 --- a/web/embed.go +++ b/web/embed.go @@ -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(".") +} diff --git a/web/handler.go b/web/handler.go index 5635c8a..b54dcf1 100644 --- a/web/handler.go +++ b/web/handler.go @@ -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) }