From a07fbfd7b7613bba556abb9290e4649f775d976c Mon Sep 17 00:00:00 2001 From: Luca Crema Date: Wed, 10 Jul 2024 14:27:11 +0200 Subject: [PATCH 1/4] Added baseurl somewhere --- internal/assets/templates/document.html | 12 ++++++------ internal/glance/glance.go | 11 ++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/internal/assets/templates/document.html b/internal/assets/templates/document.html index d126d8b..4d784bd 100644 --- a/internal/assets/templates/document.html +++ b/internal/assets/templates/document.html @@ -11,12 +11,12 @@ - - - - - - + + + + + + {{ block "document-head-after" . }}{{ end }} diff --git a/internal/glance/glance.go b/internal/glance/glance.go index 653be75..8520012 100644 --- a/internal/glance/glance.go +++ b/internal/glance/glance.go @@ -41,6 +41,7 @@ type Server struct { Host string `yaml:"host"` Port uint16 `yaml:"port"` AssetsPath string `yaml:"assets-path"` + BaseUrl string `yaml:"base-url"` StartedAt time.Time `yaml:"-"` } @@ -194,10 +195,10 @@ func (a *Application) Serve() error { // TODO: add HTTPS support mux := http.NewServeMux() - 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))) + 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))) if a.Config.Server.AssetsPath != "" { absAssetsPath, err := filepath.Abs(a.Config.Server.AssetsPath) @@ -208,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("/assets/{path...}", http.StripPrefix("/assets/", assetsFS)) + mux.Handle(fmt.Sprintf("%s/assets/{path...}", a.Config.Server.BaseUrl), http.StripPrefix("/assets/", assetsFS)) } server := http.Server{ From 23d6d411481e1eb10dc7f397691123706f48d4d9 Mon Sep 17 00:00:00 2001 From: CremaLuca Date: Wed, 10 Jul 2024 15:22:58 +0200 Subject: [PATCH 2/4] 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() } From e1161b9227623d54171eb4798fed6d26163820b7 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:08:16 +0100 Subject: [PATCH 3/4] Refactor base-url & add documentation --- docs/configuration.md | 4 ++++ internal/assets/static/main.js | 2 +- internal/assets/templates/page.html | 7 ++++--- internal/glance/glance.go | 14 +++++++++++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 478cdcd..7abafe1 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -124,6 +124,7 @@ server: | ---- | ---- | -------- | ------- | | host | string | no | | | port | number | no | 8080 | +| base-url | string | no | | | assets-path | string | no | | #### `host` @@ -132,6 +133,9 @@ The address which the server will listen on. Setting it to `localhost` means tha #### `port` A number between 1 and 65,535, so long as that port isn't already used by anything else. +#### `base-url` +The base URL that Glance is hosted under. No need to specify this unless you're using a reverse proxy and are hosting Glance under a directory. If that's the case then you can set this value to `/glance` or whatever the directory is called. Note that the forward slash (`/`) in the beginning is required unless you specify the full domain and path. + #### `assets-path` The path to a directory that will be served by the server under the `/assets/` path. This is handy for widgets like the Monitor where you have to specify an icon URL and you want to self host all the icons rather than pointing to an external source. diff --git a/internal/assets/static/main.js b/internal/assets/static/main.js index 1ac26ca..906b930 100644 --- a/internal/assets/static/main.js +++ b/internal/assets/static/main.js @@ -24,7 +24,7 @@ function throttledDebounce(callback, maxDebounceTimes, debounceDelay) { async function fetchPageContent(pageData) { // TODO: handle non 200 status codes/time outs // TODO: add retries - const response = await fetch(`${pageData.baseUrl}/api/pages/${pageData.slug}/content/`); + const response = await fetch(`${pageData.baseURL}/api/pages/${pageData.slug}/content/`); const content = await response.text(); return content; diff --git a/internal/assets/templates/page.html b/internal/assets/templates/page.html index e9b3d3f..a77cce1 100644 --- a/internal/assets/templates/page.html +++ b/internal/assets/templates/page.html @@ -6,22 +6,23 @@ {{ end }} {{ define "document-root-attrs" }}class="{{ if .App.Config.Theme.Light }}light-scheme {{ end }}{{ if ne "" .Page.Width }}page-width-{{ .Page.Width }}{{ end }}"{{ end }} + {{ 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 b2bdb44..bebb78b 100644 --- a/internal/glance/glance.go +++ b/internal/glance/glance.go @@ -43,7 +43,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"` AssetsHash string `yaml:"-"` StartedAt time.Time `yaml:"-"` // used in custom css file } @@ -131,6 +131,14 @@ func NewApplication(config *Config) (*Application, error) { } } + config = &app.Config + + if config.Server.BaseURL != "" && + config.Theme.CustomCSSFile != "" && + strings.HasPrefix(config.Theme.CustomCSSFile, "/assets/") { + config.Theme.CustomCSSFile = config.Server.BaseURL + config.Theme.CustomCSSFile + } + return app, nil } @@ -224,7 +232,7 @@ func (a *Application) HandleWidgetRequest(w http.ResponseWriter, r *http.Request } func (a *Application) AssetPath(asset string) string { - return "/static/" + a.Config.Server.AssetsHash + "/" + asset + return a.Config.Server.BaseURL + "/static/" + a.Config.Server.AssetsHash + "/" + asset } func (a *Application) Serve() error { @@ -263,7 +271,7 @@ 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, "base url", a.Config.Server.BaseUrl) + slog.Info("Starting server", "host", a.Config.Server.Host, "port", a.Config.Server.Port, "base-url", a.Config.Server.BaseURL) return server.ListenAndServe() } From 4de465e544f1d2dbcc29e4ba132f6d7ed5168e7c Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Mon, 5 Aug 2024 14:11:19 +0100 Subject: [PATCH 4/4] Add some foolproofery --- internal/glance/glance.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/glance/glance.go b/internal/glance/glance.go index bebb78b..ce7a7a0 100644 --- a/internal/glance/glance.go +++ b/internal/glance/glance.go @@ -133,6 +133,8 @@ func NewApplication(config *Config) (*Application, error) { config = &app.Config + config.Server.BaseURL = strings.TrimRight(config.Server.BaseURL, "/") + if config.Server.BaseURL != "" && config.Theme.CustomCSSFile != "" && strings.HasPrefix(config.Theme.CustomCSSFile, "/assets/") {