32 lines
821 B
Go
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)
|
|
})
|
|
}
|