mirror of
https://github.com/khairul169/garage-webui.git
synced 2026-09-15 00:43:21 +07:00
feat: rewrite backend to go
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
//go:build !prod
|
||||
// +build !prod
|
||||
|
||||
package ui
|
||||
|
||||
func ServeUI() {}
|
||||
@@ -0,0 +1,34 @@
|
||||
//go:build prod
|
||||
// +build prod
|
||||
|
||||
package ui
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
)
|
||||
|
||||
//go:embed dist
|
||||
var embeddedFs embed.FS
|
||||
|
||||
func ServeUI() {
|
||||
distFs, _ := fs.Sub(embeddedFs, "dist")
|
||||
fileServer := http.FileServer(http.FS(distFs))
|
||||
|
||||
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_path := path.Clean(r.URL.Path)[1:]
|
||||
|
||||
// Rewrite non-existing paths to index.html
|
||||
if _, err := fs.Stat(distFs, _path); err != nil {
|
||||
index, _ := fs.ReadFile(distFs, "index.html")
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(index)
|
||||
return
|
||||
}
|
||||
|
||||
fileServer.ServeHTTP(w, r)
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user