use go embed
This commit is contained in:
parent
2e48e42795
commit
c5f459329e
@ -37,14 +37,12 @@ func webPrepareClientHTML(html string) string {
|
||||
func webRegisterAssets(ui UiConfig, router *httprouter.Router) error {
|
||||
log.Println("Preparing and installing assets")
|
||||
|
||||
// Serve static assets
|
||||
assetsHandler := http.StripPrefix("/static/", http.FileServer(client.Assets))
|
||||
|
||||
// Prepare client html: Rewrite paths
|
||||
indexHTML, err := assets.ReadFile("index.html")
|
||||
indexHTMLData, err := client.Assets.ReadFile("build/index.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
indexHTML := string(indexHTMLData) // TODO: migrate to []byte
|
||||
|
||||
theme := NewTheme(ui.Theme)
|
||||
err = theme.RegisterThemeAssets(router)
|
||||
@ -56,7 +54,7 @@ func webRegisterAssets(ui UiConfig, router *httprouter.Router) error {
|
||||
indexHTML = webPrepareClientHTML(indexHTML)
|
||||
|
||||
// Register static assets
|
||||
router.Handler("GET", "/static/*path", assetsHandler)
|
||||
router.Handler("GET", "/static/*path", client.AssetsHTTPHandler("/static"))
|
||||
|
||||
// Rewrite paths
|
||||
// Serve index html as root...
|
||||
|
44
client/assets.go
Normal file
44
client/assets.go
Normal file
@ -0,0 +1,44 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Assets hold the alice-lg frontend build
|
||||
//go:embed build/*
|
||||
var Assets embed.FS
|
||||
|
||||
// AssetsHTTPHandler handles HTTP request at a specific prefix.
|
||||
// The prefix is usually /static.
|
||||
func AssetsHTTPHandler(prefix string) http.Handler {
|
||||
handler := http.FileServer(http.FS(Assets))
|
||||
|
||||
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
path := req.URL.Path
|
||||
rawPath := req.URL.RawPath
|
||||
|
||||
if !strings.HasPrefix(path, prefix) {
|
||||
handler.ServeHTTP(res, req)
|
||||
return
|
||||
}
|
||||
|
||||
// Rewrite path
|
||||
path = "build/" + strings.TrimPrefix(path, prefix)
|
||||
rawPath = "build/" + strings.TrimPrefix(rawPath, prefix)
|
||||
|
||||
// This is pretty much like the StripPrefix middleware,
|
||||
// from net/http, however we replace the prefix with `build/`.
|
||||
req1 := new(http.Request)
|
||||
*req1 = *req // clone request
|
||||
req1.URL = new(url.URL)
|
||||
*req1.URL = *req.URL
|
||||
|
||||
req1.URL.Path = path
|
||||
req1.URL.RawPath = rawPath
|
||||
|
||||
handler.ServeHTTP(res, req1)
|
||||
})
|
||||
}
|
13
client/assets_test.go
Normal file
13
client/assets_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPresenceOfIndexHTML(t *testing.T) {
|
||||
content, err := Assets.ReadFile("build/index.html")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(string(content))
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"embed"
|
||||
)
|
||||
|
||||
// Assets hold the alice-lg frontend build
|
||||
//go:embed build/*
|
||||
var Assets embed.FS
|
Loading…
x
Reference in New Issue
Block a user