aboutsummaryrefslogtreecommitdiffstats
path: root/web/frontend.go
diff options
context:
space:
mode:
authorAdam Mathes <adam@adammathes.com>2026-02-15 17:30:34 -0800
committerAdam Mathes <adam@adammathes.com>2026-02-15 17:30:34 -0800
commit90c1a68d6478138f538094fc83e48da8ddd21fa0 (patch)
tree90c5e03ec49bb0790ca849f6dac09786cd93bce3 /web/frontend.go
parent7ceec2469ecb047ed8f9c8e2149323d8500773e2 (diff)
downloadneko-90c1a68d6478138f538094fc83e48da8ddd21fa0.tar.gz
neko-90c1a68d6478138f538094fc83e48da8ddd21fa0.tar.bz2
neko-90c1a68d6478138f538094fc83e48da8ddd21fa0.zip
Scaffold Vanilla JS Frontend (v3): Create directory, update Makefile/web.go, embed dist/v3
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))
}