@@ -22,11 +22,13 @@ import (
2222 "bytes"
2323 _ "embed"
2424 "net/http"
25+ "strings"
2526 "text/template"
2627)
2728
2829// Config represents the configuration of the web listener.
2930type LandingConfig struct {
31+ RoutePrefix string // The route prefix for the exporter.
3032 HeaderColor string // Used for the landing page header.
3133 CSS string // CSS style tag for the landing page.
3234 Name string // The name of the exporter, generally suffixed by _exporter.
@@ -62,6 +64,7 @@ type LandingLinks struct {
6264
6365type LandingPageHandler struct {
6466 landingPage []byte
67+ routePrefix string
6568}
6669
6770var (
@@ -93,6 +96,15 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) {
9396 }
9497 c .CSS = buf .String ()
9598 }
99+ if c .RoutePrefix == "" {
100+ c .RoutePrefix = "/"
101+ } else if ! strings .HasSuffix (c .RoutePrefix , "/" ) {
102+ c .RoutePrefix += "/"
103+ }
104+ // Strip leading '/' from Links if present
105+ for i , link := range c .Links {
106+ c .Links [i ].Address = strings .TrimPrefix (link .Address , "/" )
107+ }
96108 t := template .Must (template .New ("landing page" ).Parse (landingPagehtmlContent ))
97109
98110 buf .Reset ()
@@ -102,11 +114,12 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) {
102114
103115 return & LandingPageHandler {
104116 landingPage : buf .Bytes (),
117+ routePrefix : c .RoutePrefix ,
105118 }, nil
106119}
107120
108121func (h * LandingPageHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
109- if r .URL .Path != "/" {
122+ if r .URL .Path != h . routePrefix {
110123 http .NotFound (w , r )
111124 return
112125 }
0 commit comments