newdrive/internal/adapters/webassets/handler.go
Elijah f24e96efa7
All checks were successful
CI / Backend (push) Successful in 29s
CI / Frontend (push) Successful in 9m27s
CI / Contracts and repository policy (push) Successful in 7s
CI / Container (push) Successful in 18s
Harden CI and switch release builds to tagged immutable images
2026-07-15 19:21:07 -07:00

32 lines
821 B
Go

// Package webassets serves the compiled frontend without giving transport code filesystem access.
package webassets
import (
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
)
func New(root string) http.Handler {
files := os.DirFS(root)
fileServer := http.FileServer(http.FS(files))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requested := strings.TrimPrefix(filepath.Clean(r.URL.Path), string(filepath.Separator))
if requested == "." {
requested = "index.html"
}
if _, err := fs.Stat(files, requested); err == nil {
fileServer.ServeHTTP(w, r)
return
}
if _, err := fs.Stat(files, "index.html"); err == nil {
r.URL.Path = "/"
fileServer.ServeHTTP(w, r)
return
}
http.Error(w, "Drive web assets are not built", http.StatusServiceUnavailable)
})
}