From 29a866361dbf0ca5f4dbcf6870249c7a3ab5474e Mon Sep 17 00:00:00 2001 From: kwilt Date: Fri, 10 Jan 2025 15:25:45 -0600 Subject: [PATCH 1/4] Add argument to override serving landing page on root path, allowing for --web.external-url to be used with LandingPageHandler Signed-off-by: kwilt --- web/landing_page.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/web/landing_page.go b/web/landing_page.go index d417c15e..10fe5139 100644 --- a/web/landing_page.go +++ b/web/landing_page.go @@ -61,7 +61,8 @@ type LandingLinks struct { } type LandingPageHandler struct { - landingPage []byte + landingPage []byte + routePrefixed bool } var ( @@ -71,7 +72,7 @@ var ( landingPagecssContent string ) -func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) { +func NewLandingPage(c LandingConfig, routePrefixed bool) (*LandingPageHandler, error) { var buf bytes.Buffer length := 0 @@ -101,12 +102,13 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) { } return &LandingPageHandler{ - landingPage: buf.Bytes(), + landingPage: buf.Bytes(), + routePrefixed: routePrefixed, }, nil } func (h *LandingPageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { + if !h.routePrefixed && r.URL.Path != "/" { http.NotFound(w, r) return } From 4ed0440436b94fcce8fb5edec8726bd5fae1d7a0 Mon Sep 17 00:00:00 2001 From: kwilt Date: Fri, 10 Jan 2025 20:57:01 -0600 Subject: [PATCH 2/4] add RoutePrefix to LandingConfig, update html to use the new RoutePrefix when provided Signed-off-by: kwilt --- web/landing_page.go | 21 ++++++++++++--------- web/landing_page.html | 8 ++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/web/landing_page.go b/web/landing_page.go index 10fe5139..d5c6b624 100644 --- a/web/landing_page.go +++ b/web/landing_page.go @@ -22,11 +22,13 @@ import ( "bytes" _ "embed" "net/http" + "strings" "text/template" ) // Config represents the configuration of the web listener. type LandingConfig struct { + RoutePrefix string // The route prefix for the exporter. HeaderColor string // Used for the landing page header. CSS string // CSS style tag for the landing page. Name string // The name of the exporter, generally suffixed by _exporter. @@ -61,8 +63,7 @@ type LandingLinks struct { } type LandingPageHandler struct { - landingPage []byte - routePrefixed bool + landingPage []byte } var ( @@ -72,7 +73,7 @@ var ( landingPagecssContent string ) -func NewLandingPage(c LandingConfig, routePrefixed bool) (*LandingPageHandler, error) { +func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) { var buf bytes.Buffer length := 0 @@ -94,6 +95,13 @@ func NewLandingPage(c LandingConfig, routePrefixed bool) (*LandingPageHandler, e } c.CSS = buf.String() } + if c.RoutePrefix == "" { + c.RoutePrefix = "/" + } + // Strip leading '/' from Links if present + for i, link := range c.Links { + c.Links[i].Address = strings.TrimPrefix(link.Address, "/") + } t := template.Must(template.New("landing page").Parse(landingPagehtmlContent)) buf.Reset() @@ -102,16 +110,11 @@ func NewLandingPage(c LandingConfig, routePrefixed bool) (*LandingPageHandler, e } return &LandingPageHandler{ - landingPage: buf.Bytes(), - routePrefixed: routePrefixed, + landingPage: buf.Bytes(), }, nil } func (h *LandingPageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if !h.routePrefixed && r.URL.Path != "/" { - http.NotFound(w, r) - return - } w.Header().Add("Content-Type", "text/html; charset=UTF-8") w.Write(h.landingPage) } diff --git a/web/landing_page.html b/web/landing_page.html index e1ac0aec..53efdc60 100644 --- a/web/landing_page.html +++ b/web/landing_page.html @@ -15,13 +15,13 @@

{{.Name}}

    {{ range .Links }} -
  • {{.Text}}{{if .Description}}: {{.Description}}{{end}}
  • +
  • {{.Text}}{{if .Description}}: {{.Description}}{{end}}
  • {{ end }}
{{ if .Form.Action }}
-
+ {{ range .Form.Inputs }}  
{{ end }} @@ -33,8 +33,8 @@

{{.Name}}

Download a detailed report of resource usage (pprof format, from the Go runtime): To visualize and share profiles you can upload to pprof.me
From 15f17b678155c30715d78c531076bc3ba848ea87 Mon Sep 17 00:00:00 2001 From: kwilt Date: Sat, 11 Jan 2025 15:21:26 -0600 Subject: [PATCH 3/4] 404 anything that doesn't match routePrefix Signed-off-by: kwilt --- web/landing_page.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/web/landing_page.go b/web/landing_page.go index d5c6b624..06a4c28e 100644 --- a/web/landing_page.go +++ b/web/landing_page.go @@ -64,6 +64,7 @@ type LandingLinks struct { type LandingPageHandler struct { landingPage []byte + routePrefix string } var ( @@ -111,10 +112,15 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) { return &LandingPageHandler{ landingPage: buf.Bytes(), + routePrefix: c.RoutePrefix, }, nil } func (h *LandingPageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != h.routePrefix { + http.NotFound(w, r) + return + } w.Header().Add("Content-Type", "text/html; charset=UTF-8") w.Write(h.landingPage) } From a81341a5025ccdc993b1bbbdbadb01f6b893a680 Mon Sep 17 00:00:00 2001 From: kwilt Date: Sun, 12 Jan 2025 20:58:10 -0600 Subject: [PATCH 4/4] ensure RoutePrefix ends in trailing slash Signed-off-by: kwilt --- web/landing_page.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/web/landing_page.go b/web/landing_page.go index 06a4c28e..86ee6c3b 100644 --- a/web/landing_page.go +++ b/web/landing_page.go @@ -98,6 +98,8 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) { } if c.RoutePrefix == "" { c.RoutePrefix = "/" + } else if !strings.HasSuffix(c.RoutePrefix, "/") { + c.RoutePrefix += "/" } // Strip leading '/' from Links if present for i, link := range c.Links {