From e762f11257a691c03a668de48e520154e36a3e42 Mon Sep 17 00:00:00 2001 From: Annika Hannig Date: Mon, 25 Jul 2022 18:48:19 +0200 Subject: [PATCH] use and serve build --- pkg/http/web.go | 16 +++++++++------- ui/assets.go | 11 ++++++----- ui/public/index.html | 1 + ui/public/robots.txt | 3 --- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pkg/http/web.go b/pkg/http/web.go index cff36a0..629bc9d 100644 --- a/pkg/http/web.go +++ b/pkg/http/web.go @@ -9,7 +9,7 @@ import ( "github.com/julienschmidt/httprouter" - "github.com/alice-lg/alice-lg/client" + "github.com/alice-lg/alice-lg/ui" ) // Web Client @@ -26,11 +26,11 @@ func (s *Server) webPrepareClientHTML( // Replace paths and tags rewriter := strings.NewReplacer( // Paths - "js/", "/static/js/", - "css/", "/static/css/", + // "js/", "/static/js/", + // "css/", "/static/css/", // Tags - "APP_VERSION", status.Version, + "###APP_VERSION###", status.Version, ) html = rewriter.Replace(html) return html @@ -45,11 +45,11 @@ func (s *Server) webRegisterAssets( log.Println("Preparing and installing assets") // Prepare client html: Rewrite paths - indexHTMLData, err := client.Assets.ReadFile("build/index.html") + indexHTMLData, err := ui.Assets.ReadFile("build/index.html") if err != nil { return err } - indexHTML := string(indexHTMLData) // TODO: migrate to []byte + indexHTML := string(indexHTMLData) theme := NewTheme(s.cfg.UI.Theme) err = theme.RegisterThemeAssets(router) @@ -61,7 +61,9 @@ func (s *Server) webRegisterAssets( indexHTML = s.webPrepareClientHTML(ctx, indexHTML) // Register static assets - router.Handler("GET", "/static/*path", client.AssetsHTTPHandler("/static")) + router.Handler("GET", "/manifest.json", ui.AssetsHTTPHandler("/")) + router.Handler("GET", "/static/*path", ui.AssetsHTTPHandler("/static")) + router.Handler("GET", "/assets/*path", ui.AssetsHTTPHandler("/assets")) // Rewrite paths // Serve index html as root... diff --git a/ui/assets.go b/ui/assets.go index 3f3bb1d..535fd80 100644 --- a/ui/assets.go +++ b/ui/assets.go @@ -4,6 +4,7 @@ import ( "embed" "net/http" "net/url" + "path" "strings" ) @@ -17,17 +18,17 @@ 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 + reqPath := req.URL.Path rawPath := req.URL.RawPath - if !strings.HasPrefix(path, prefix) { + if !strings.HasPrefix(reqPath, prefix) { handler.ServeHTTP(res, req) return } // Rewrite path - path = "build/" + strings.TrimPrefix(path, prefix) - rawPath = "build/" + strings.TrimPrefix(rawPath, prefix) + reqPath = path.Join("build/", reqPath) + rawPath = path.Join("build/", rawPath) // This is pretty much like the StripPrefix middleware, // from net/http, however we replace the prefix with `build/`. @@ -36,7 +37,7 @@ func AssetsHTTPHandler(prefix string) http.Handler { req1.URL = new(url.URL) *req1.URL = *req.URL - req1.URL.Path = path + req1.URL.Path = reqPath req1.URL.RawPath = rawPath handler.ServeHTTP(res, req1) diff --git a/ui/public/index.html b/ui/public/index.html index 6130466..0b82a9a 100644 --- a/ui/public/index.html +++ b/ui/public/index.html @@ -2,6 +2,7 @@ + diff --git a/ui/public/robots.txt b/ui/public/robots.txt index e9e57dc..e69de29 100644 --- a/ui/public/robots.txt +++ b/ui/public/robots.txt @@ -1,3 +0,0 @@ -# https://www.robotstxt.org/robotstxt.html -User-agent: * -Disallow: