Skip to content

Commit 9a0befe

Browse files
authored
Serve behind route prefix (#283)
Add RoutePrefix to LandingConfig, update html to use the new RoutePrefix when provided --------- Signed-off-by: kwilt <[email protected]>
1 parent 574d848 commit 9a0befe

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

web/landing_page.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -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.
2930
type 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

6365
type LandingPageHandler struct {
6466
landingPage []byte
67+
routePrefix string
6568
}
6669

6770
var (
@@ -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

108121
func (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
}

web/landing_page.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ <h1>{{.Name}}</h1>
1515
<div>
1616
<ul>
1717
{{ range .Links }}
18-
<li><a href="{{ .Address }}">{{.Text}}</a>{{if .Description}}: {{.Description}}{{end}}</li>
18+
<li><a href="{{if $.RoutePrefix}}{{ $.RoutePrefix }}{{end}}{{ .Address }}">{{.Text}}</a>{{if .Description}}: {{.Description}}{{end}}</li>
1919
{{ end }}
2020
</ul>
2121
</div>
2222
{{ if .Form.Action }}
2323
<div>
24-
<form action="{{ .Form.Action}}">
24+
<form action="{{if $.RoutePrefix}}{{ $.RoutePrefix }}{{end}}{{ .Form.Action }}">
2525
{{ range .Form.Inputs }}
2626
<label>{{ .Label }}:</label>&nbsp;<input type="{{ .Type }}" name="{{ .Name }}" placeholder="{{ .Placeholder }}" value="{{ .Value }}"><br>
2727
{{ end }}
@@ -33,8 +33,8 @@ <h1>{{.Name}}</h1>
3333
<div id="pprof">
3434
Download a detailed report of resource usage (pprof format, from the Go runtime):
3535
<ul>
36-
<li><a href="debug/pprof/heap">heap usage (memory)</a>
37-
<li><a href="debug/pprof/profile?seconds=60">CPU usage (60 second profile)</a>
36+
<li><a href="{{if $.RoutePrefix}}{{ $.RoutePrefix }}{{end}}debug/pprof/heap">heap usage (memory)</a>
37+
<li><a href="{{if $.RoutePrefix}}{{ $.RoutePrefix }}{{end}}debug/pprof/profile?seconds=60">CPU usage (60 second profile)</a>
3838
</ul>
3939
To visualize and share profiles you can upload to <a href="https://pprof.me" target="_blank">pprof.me</a>
4040
</div>

0 commit comments

Comments
 (0)