38 lines
816 B
Go
38 lines
816 B
Go
package static
|
|
|
|
import (
|
|
"embed"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed app/dist
|
|
var appFS embed.FS
|
|
|
|
var Handler = func() http.HandlerFunc {
|
|
distFS, err := fs.Sub(appFS, "app/dist")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
fileServer := http.FileServer(http.FS(distFS))
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
path := strings.TrimPrefix(r.URL.Path, "/")
|
|
_, err := distFS.Open(path)
|
|
if err == nil || path == "" {
|
|
fileServer.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
indexFile, err := distFS.Open("index.html")
|
|
if err != nil {
|
|
http.Error(w, "frontend build not found", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer func() { _ = indexFile.Close() }()
|
|
stat, _ := indexFile.Stat()
|
|
http.ServeContent(w, r, "index.html", stat.ModTime(), indexFile.(io.ReadSeeker))
|
|
}
|
|
}()
|