You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

45 lines
800 B

//go:build !dev
package main
import (
"embed"
"io/fs"
"net/http"
"path/filepath"
)
//go:embed web/dist
var webFS embed.FS
func spaHandler() http.Handler {
return &spa{root: "web/dist", fs: webFS}
}
type spa struct {
root string
fs embed.FS
}
func (h *spa) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(h.root, r.URL.Path)
f, err := h.fs.Open(path)
if err != nil {
index, readErr := h.fs.ReadFile(filepath.Join(h.root, "index.html"))
if readErr != nil {
http.Error(w, "not found", 404)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(index)
return
}
f.Close()
sub, err := fs.Sub(h.fs, h.root)
if err != nil {
http.Error(w, "not found", 404)
return
}
http.FileServer(http.FS(sub)).ServeHTTP(w, r)
}