浏览代码

Merge pull request #167 from CremaLuca/main

feat: serving behind reverse proxy
Svilen Markov 1 年之前
父节点
当前提交
60b73aff24
共有 4 个文件被更改,包括 24 次插入6 次删除
  1. 4 0
      docs/configuration.md
  2. 3 3
      internal/assets/static/main.js
  3. 3 1
      internal/assets/templates/page.html
  4. 14 2
      internal/glance/glance.go

+ 4 - 0
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.
 

+ 3 - 3
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;
@@ -588,7 +588,7 @@ function setupClocks() {
 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;
 

+ 3 - 1
internal/assets/templates/page.html

@@ -6,11 +6,13 @@
 <script>
     const pageData = {
         slug: "{{ .Page.Slug }}",
+        baseURL: "{{ .App.Config.Server.BaseURL }}",
     };
 </script>
 {{ 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 }}
@@ -20,7 +22,7 @@
 
 {{ define "navigation-links" }}
 {{ range .App.Config.Pages }}
-<a href="/{{ .Slug }}" class="nav-item{{ if eq .Slug $.Page.Slug }} nav-item-current{{ end }}">{{ .Title }}</a>
+<a href="{{ $.App.Config.Server.BaseURL }}/{{ .Slug }}" class="nav-item{{ if eq .Slug $.Page.Slug }} nav-item-current{{ end }}">{{ .Title }}</a>
 {{ end }}
 {{ end }}
 

+ 14 - 2
internal/glance/glance.go

@@ -43,6 +43,7 @@ type Server struct {
 	Host       string    `yaml:"host"`
 	Port       uint16    `yaml:"port"`
 	AssetsPath string    `yaml:"assets-path"`
+	BaseURL    string    `yaml:"base-url"`
 	AssetsHash string    `yaml:"-"`
 	StartedAt  time.Time `yaml:"-"` // used in custom css file
 }
@@ -130,6 +131,16 @@ 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/") {
+		config.Theme.CustomCSSFile = config.Server.BaseURL + config.Theme.CustomCSSFile
+	}
+
 	return app, nil
 }
 
@@ -223,7 +234,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 {
@@ -262,6 +273,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)
+	slog.Info("Starting server", "host", a.Config.Server.Host, "port", a.Config.Server.Port, "base-url", a.Config.Server.BaseURL)
+
 	return server.ListenAndServe()
 }