aboutsummaryrefslogtreecommitdiffstats
path: root/web/frontend.go
diff options
context:
space:
mode:
Diffstat (limited to 'web/frontend.go')
-rw-r--r--web/frontend.go69
1 files changed, 36 insertions, 33 deletions
diff --git a/web/frontend.go b/web/frontend.go
index d77c974..00074ec 100644
--- a/web/frontend.go
+++ b/web/frontend.go
@@ -8,46 +8,49 @@ import (
"strings"
)
-func ServeFrontend(w http.ResponseWriter, r *http.Request) {
- // Use fs.Sub to treat dist/v2 as the root
- box, err := fs.Sub(frontendFiles, "dist/v2")
- if err != nil {
- http.Error(w, "frontend not found", http.StatusNotFound)
- return
- }
+func ServeFrontend(distDir string) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ // Use fs.Sub to treat distDir as the root
+ box, err := fs.Sub(frontendFiles, distDir)
+ if err != nil {
+ http.Error(w, "frontend not found", http.StatusNotFound)
+ return
+ }
- // Get the file path from the URL
- path := r.URL.Path
- path = strings.TrimPrefix(path, "/")
+ // Get the file path from the URL
+ path := r.URL.Path
+ path = strings.TrimPrefix(path, "/")
- // If path is empty, it's index.html
- if path == "" {
- path = "index.html"
- }
+ // If path is empty, it's index.html
+ if path == "" {
+ path = "index.html"
+ }
- // Try to open the file
- f, err := box.Open(path)
- if err != nil {
- // If file not found, serve index.html for client-side routing
- if !strings.Contains(filepath.Base(path), ".") {
- f, err = box.Open("index.html")
- if err != nil {
- http.Error(w, "frontend not found", http.StatusNotFound)
+ // Try to open the file
+ f, err := box.Open(path)
+ if err != nil {
+ // If file not found, serve index.html for client-side routing
+ // but only if it looks like a route (no extension)
+ if !strings.Contains(filepath.Base(path), ".") {
+ f, err = box.Open("index.html")
+ if err != nil {
+ http.Error(w, "frontend not found", http.StatusNotFound)
+ return
+ }
+ path = "index.html"
+ } else {
+ http.Error(w, "not found", http.StatusNotFound)
return
}
- path = "index.html"
- } else {
- http.Error(w, "not found", http.StatusNotFound)
+ }
+ defer func() { _ = f.Close() }()
+
+ d, err := f.Stat()
+ if err != nil {
+ http.Error(w, "internal error", http.StatusInternalServerError)
return
}
- }
- defer func() { _ = f.Close() }()
- d, err := f.Stat()
- if err != nil {
- http.Error(w, "internal error", http.StatusInternalServerError)
- return
+ http.ServeContent(w, r, path, d.ModTime(), f.(io.ReadSeeker))
}
-
- http.ServeContent(w, r, path, d.ModTime(), f.(io.ReadSeeker))
}