From 23d6d411481e1eb10dc7f397691123706f48d4d9 Mon Sep 17 00:00:00 2001 From: CremaLuca Date: Wed, 10 Jul 2024 15:22:58 +0200 Subject: [PATCH] fix(serve): using paths without base url --- internal/assets/static/main.js | 6 +++--- internal/assets/templates/page.html | 5 +++-- internal/glance/glance.go | 14 +++++++------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/internal/assets/static/main.js b/internal/assets/static/main.js index 9818ecf..5426c29 100644 --- a/internal/assets/static/main.js +++ b/internal/assets/static/main.js @@ -21,10 +21,10 @@ function throttledDebounce(callback, maxDebounceTimes, debounceDelay) { }; -async function fetchPageContent(pageSlug) { +async function fetchPageContent(pageData) { // TODO: handle non 200 status codes/time outs // TODO: add retries - const response = await fetch(`/api/pages/${pageSlug}/content/`); + const response = await fetch(`${pageData.baseUrl}/api/pages/${pageData.slug}/content/`); const content = await response.text(); return content; @@ -336,7 +336,7 @@ function setupCollapsibleGrids() { async function setupPage() { const pageElement = document.getElementById("page"); const pageContentElement = document.getElementById("page-content"); - const pageContent = await fetchPageContent(pageData.slug); + const pageContent = await fetchPageContent(pageData); pageContentElement.innerHTML = pageContent; diff --git a/internal/assets/templates/page.html b/internal/assets/templates/page.html index 98bb111..42e6bb2 100644 --- a/internal/assets/templates/page.html +++ b/internal/assets/templates/page.html @@ -6,6 +6,7 @@ {{ end }} @@ -14,13 +15,13 @@ {{ define "document-head-after" }} {{ template "page-style-overrides.gotmpl" . }} {{ if ne "" .App.Config.Theme.CustomCSSFile }} - + {{ end }} {{ end }} {{ define "navigation-links" }} {{ range .App.Config.Pages }} -{{ .Title }} +{{ .Title }} {{ end }} {{ end }} diff --git a/internal/glance/glance.go b/internal/glance/glance.go index 8520012..dd9e40b 100644 --- a/internal/glance/glance.go +++ b/internal/glance/glance.go @@ -41,7 +41,7 @@ type Server struct { Host string `yaml:"host"` Port uint16 `yaml:"port"` AssetsPath string `yaml:"assets-path"` - BaseUrl string `yaml:"base-url"` + BaseUrl string `yaml:"base-url"` StartedAt time.Time `yaml:"-"` } @@ -195,10 +195,10 @@ func (a *Application) Serve() error { // TODO: add HTTPS support mux := http.NewServeMux() - mux.HandleFunc(fmt.Sprintf("GET %s/{$}", a.Config.Server.BaseUrl), a.HandlePageRequest) - mux.HandleFunc(fmt.Sprintf("GET %s/{page}", a.Config.Server.BaseUrl), a.HandlePageRequest) - mux.HandleFunc(fmt.Sprintf("GET %s/api/pages/{page}/content/{$}", a.Config.Server.BaseUrl), a.HandlePageContentRequest) - mux.Handle(fmt.Sprintf("GET %s/static/{path...}", a.Config.Server.BaseUrl), http.StripPrefix(fmt.Sprintf("%s/static/", a.Config.Server.BaseUrl), FileServerWithCache(http.FS(assets.PublicFS), 2*time.Hour))) + mux.HandleFunc("GET /{$}", a.HandlePageRequest) + mux.HandleFunc("GET /{page}", a.HandlePageRequest) + mux.HandleFunc("GET /api/pages/{page}/content/{$}", a.HandlePageContentRequest) + mux.Handle("GET /static/{path...}", http.StripPrefix("/static/", FileServerWithCache(http.FS(assets.PublicFS), 2*time.Hour))) if a.Config.Server.AssetsPath != "" { absAssetsPath, err := filepath.Abs(a.Config.Server.AssetsPath) @@ -209,7 +209,7 @@ func (a *Application) Serve() error { slog.Info("Serving assets", "path", absAssetsPath) assetsFS := FileServerWithCache(http.Dir(a.Config.Server.AssetsPath), 2*time.Hour) - mux.Handle(fmt.Sprintf("%s/assets/{path...}", a.Config.Server.BaseUrl), http.StripPrefix("/assets/", assetsFS)) + mux.Handle("/assets/{path...}", http.StripPrefix("/assets/", assetsFS)) } server := http.Server{ @@ -219,6 +219,6 @@ func (a *Application) Serve() error { a.Config.Server.StartedAt = time.Now() - slog.Info("Starting server", "host", a.Config.Server.Host, "port", a.Config.Server.Port) + slog.Info("Starting server", "host", a.Config.Server.Host, "port", a.Config.Server.Port, "base url", a.Config.Server.BaseUrl) return server.ListenAndServe() }