Skip to content

Commit

Permalink
Serve behind route prefix (#283)
Browse files Browse the repository at this point in the history
Add RoutePrefix to LandingConfig, update html to use the new RoutePrefix when provided

---------

Signed-off-by: kwilt <[email protected]>
  • Loading branch information
kwilt authored Jan 17, 2025
1 parent 574d848 commit 9a0befe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 14 additions & 1 deletion web/landing_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -62,6 +64,7 @@ type LandingLinks struct {

type LandingPageHandler struct {
landingPage []byte
routePrefix string
}

var (
Expand Down Expand Up @@ -93,6 +96,15 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) {
}
c.CSS = buf.String()
}
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 {
c.Links[i].Address = strings.TrimPrefix(link.Address, "/")
}
t := template.Must(template.New("landing page").Parse(landingPagehtmlContent))

buf.Reset()
Expand All @@ -102,11 +114,12 @@ 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 != "/" {
if r.URL.Path != h.routePrefix {
http.NotFound(w, r)
return
}
Expand Down
8 changes: 4 additions & 4 deletions web/landing_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ <h1>{{.Name}}</h1>
<div>
<ul>
{{ range .Links }}
<li><a href="{{ .Address }}">{{.Text}}</a>{{if .Description}}: {{.Description}}{{end}}</li>
<li><a href="{{if $.RoutePrefix}}{{ $.RoutePrefix }}{{end}}{{ .Address }}">{{.Text}}</a>{{if .Description}}: {{.Description}}{{end}}</li>
{{ end }}
</ul>
</div>
{{ if .Form.Action }}
<div>
<form action="{{ .Form.Action}}">
<form action="{{if $.RoutePrefix}}{{ $.RoutePrefix }}{{end}}{{ .Form.Action }}">
{{ range .Form.Inputs }}
<label>{{ .Label }}:</label>&nbsp;<input type="{{ .Type }}" name="{{ .Name }}" placeholder="{{ .Placeholder }}" value="{{ .Value }}"><br>
{{ end }}
Expand All @@ -33,8 +33,8 @@ <h1>{{.Name}}</h1>
<div id="pprof">
Download a detailed report of resource usage (pprof format, from the Go runtime):
<ul>
<li><a href="debug/pprof/heap">heap usage (memory)</a>
<li><a href="debug/pprof/profile?seconds=60">CPU usage (60 second profile)</a>
<li><a href="{{if $.RoutePrefix}}{{ $.RoutePrefix }}{{end}}debug/pprof/heap">heap usage (memory)</a>
<li><a href="{{if $.RoutePrefix}}{{ $.RoutePrefix }}{{end}}debug/pprof/profile?seconds=60">CPU usage (60 second profile)</a>
</ul>
To visualize and share profiles you can upload to <a href="https://pprof.me" target="_blank">pprof.me</a>
</div>
Expand Down

0 comments on commit 9a0befe

Please sign in to comment.