@@ -22,11 +22,13 @@ import (
22
22
"bytes"
23
23
_ "embed"
24
24
"net/http"
25
+ "strings"
25
26
"text/template"
26
27
)
27
28
28
29
// Config represents the configuration of the web listener.
29
30
type LandingConfig struct {
31
+ RoutePrefix string // The route prefix for the exporter.
30
32
HeaderColor string // Used for the landing page header.
31
33
CSS string // CSS style tag for the landing page.
32
34
Name string // The name of the exporter, generally suffixed by _exporter.
@@ -62,6 +64,7 @@ type LandingLinks struct {
62
64
63
65
type LandingPageHandler struct {
64
66
landingPage []byte
67
+ routePrefix string
65
68
}
66
69
67
70
var (
@@ -93,6 +96,15 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) {
93
96
}
94
97
c .CSS = buf .String ()
95
98
}
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
+ }
96
108
t := template .Must (template .New ("landing page" ).Parse (landingPagehtmlContent ))
97
109
98
110
buf .Reset ()
@@ -102,11 +114,12 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) {
102
114
103
115
return & LandingPageHandler {
104
116
landingPage : buf .Bytes (),
117
+ routePrefix : c .RoutePrefix ,
105
118
}, nil
106
119
}
107
120
108
121
func (h * LandingPageHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
109
- if r .URL .Path != "/" {
122
+ if r .URL .Path != h . routePrefix {
110
123
http .NotFound (w , r )
111
124
return
112
125
}
0 commit comments