aboutsummaryrefslogtreecommitdiffstats
path: root/web/frontend.go
blob: 00074ecd7ee98b40e3dcaff551d525fac71df10b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package web

import (
	"io"
	"io/fs"
	"net/http"
	"path/filepath"
	"strings"
)

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, "/")

		// 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
			// 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
			}
		}
		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))
	}
}