Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve behind route prefix #283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions 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 @@ -93,6 +95,13 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) {
}
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()
Expand All @@ -106,10 +115,6 @@ func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) {
}

func (h *LandingPageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
w.Write(h.landingPage)
}
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