use and serve build

This commit is contained in:
Annika Hannig 2022-07-25 18:48:19 +02:00
parent acc37d53d8
commit e762f11257
4 changed files with 16 additions and 15 deletions

View File

@ -9,7 +9,7 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/alice-lg/alice-lg/client" "github.com/alice-lg/alice-lg/ui"
) )
// Web Client // Web Client
@ -26,11 +26,11 @@ func (s *Server) webPrepareClientHTML(
// Replace paths and tags // Replace paths and tags
rewriter := strings.NewReplacer( rewriter := strings.NewReplacer(
// Paths // Paths
"js/", "/static/js/", // "js/", "/static/js/",
"css/", "/static/css/", // "css/", "/static/css/",
// Tags // Tags
"APP_VERSION", status.Version, "###APP_VERSION###", status.Version,
) )
html = rewriter.Replace(html) html = rewriter.Replace(html)
return html return html
@ -45,11 +45,11 @@ func (s *Server) webRegisterAssets(
log.Println("Preparing and installing assets") log.Println("Preparing and installing assets")
// Prepare client html: Rewrite paths // Prepare client html: Rewrite paths
indexHTMLData, err := client.Assets.ReadFile("build/index.html") indexHTMLData, err := ui.Assets.ReadFile("build/index.html")
if err != nil { if err != nil {
return err return err
} }
indexHTML := string(indexHTMLData) // TODO: migrate to []byte indexHTML := string(indexHTMLData)
theme := NewTheme(s.cfg.UI.Theme) theme := NewTheme(s.cfg.UI.Theme)
err = theme.RegisterThemeAssets(router) err = theme.RegisterThemeAssets(router)
@ -61,7 +61,9 @@ func (s *Server) webRegisterAssets(
indexHTML = s.webPrepareClientHTML(ctx, indexHTML) indexHTML = s.webPrepareClientHTML(ctx, indexHTML)
// Register static assets // 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 // Rewrite paths
// Serve index html as root... // Serve index html as root...

View File

@ -4,6 +4,7 @@ import (
"embed" "embed"
"net/http" "net/http"
"net/url" "net/url"
"path"
"strings" "strings"
) )
@ -17,17 +18,17 @@ func AssetsHTTPHandler(prefix string) http.Handler {
handler := http.FileServer(http.FS(Assets)) handler := http.FileServer(http.FS(Assets))
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
path := req.URL.Path reqPath := req.URL.Path
rawPath := req.URL.RawPath rawPath := req.URL.RawPath
if !strings.HasPrefix(path, prefix) { if !strings.HasPrefix(reqPath, prefix) {
handler.ServeHTTP(res, req) handler.ServeHTTP(res, req)
return return
} }
// Rewrite path // Rewrite path
path = "build/" + strings.TrimPrefix(path, prefix) reqPath = path.Join("build/", reqPath)
rawPath = "build/" + strings.TrimPrefix(rawPath, prefix) rawPath = path.Join("build/", rawPath)
// This is pretty much like the StripPrefix middleware, // This is pretty much like the StripPrefix middleware,
// from net/http, however we replace the prefix with `build/`. // 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 = new(url.URL)
*req1.URL = *req.URL *req1.URL = *req.URL
req1.URL.Path = path req1.URL.Path = reqPath
req1.URL.RawPath = rawPath req1.URL.RawPath = rawPath
handler.ServeHTTP(res, req1) handler.ServeHTTP(res, req1)

View File

@ -2,6 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="alice-version" content="###APP_VERSION###" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="stylesheet" href="%PUBLIC_URL%/assets/fonts/source-sans-pro.css" /> <link rel="stylesheet" href="%PUBLIC_URL%/assets/fonts/source-sans-pro.css" />

View File

@ -1,3 +0,0 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow: