-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a configurable exporer landing page generator. Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu> Co-authored-by: Ben Kochie <superq@gmail.com>
- Loading branch information
1 parent
3d42407
commit 00162f9
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
body { | ||
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,Liberation Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji; | ||
margin: 0; | ||
} | ||
nav { | ||
background-color: {{.HeaderColor}}; | ||
color: #fff; | ||
font-size: 2rem; | ||
padding: 1rem; | ||
} | ||
.main { | ||
padding: 1rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2023 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build !genassets | ||
// +build !genassets | ||
|
||
//go:generate go run -tags genassets gen_assets.go | ||
|
||
package web | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"net/http" | ||
"text/template" | ||
) | ||
|
||
// Config represents the configuration of the web listener. | ||
type LandingConfig struct { | ||
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. | ||
Description string // A short description about the exporter. | ||
Links []LandingLinks // Links displayed on the landing page. | ||
Version string // The version displayed. | ||
} | ||
|
||
type LandingLinks struct { | ||
Address string // The URL the link points to. | ||
Text string // The text of the link. | ||
Description string // A descriptive textfor the link. | ||
} | ||
|
||
type LandingPageHandler struct { | ||
landingPage []byte | ||
} | ||
|
||
var ( | ||
//go:embed landing_page.html | ||
landingPagehtmlContent string | ||
//go:embed landing_page.css | ||
landingPagecssContent string | ||
) | ||
|
||
func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) { | ||
var buf bytes.Buffer | ||
if c.CSS == "" { | ||
if c.HeaderColor == "" { | ||
// Default to Prometheus orange. | ||
c.HeaderColor = "#e6522c" | ||
} | ||
cssTemplate := template.Must(template.New("landing css").Parse(landingPagecssContent)) | ||
if err := cssTemplate.Execute(&buf, c); err != nil { | ||
return nil, err | ||
} | ||
c.CSS = buf.String() | ||
} | ||
t := template.Must(template.New("landing page").Parse(landingPagehtmlContent)) | ||
|
||
buf.Reset() | ||
if err := t.Execute(&buf, c); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &LandingPageHandler{ | ||
landingPage: buf.Bytes(), | ||
}, nil | ||
} | ||
|
||
func (h *LandingPageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
w.Write(h.landingPage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>{{.Name}}</title> | ||
<style>{{.CSS}}</style> | ||
</head> | ||
<body> | ||
<nav>{{.Name}}</nav> | ||
<div class="main"> | ||
{{if .Description}}<h3>{{.Description}}</h3>{{end}} | ||
{{if .Version}}<div>Version: {{.Version}}</div>{{end}} | ||
<div><ul> | ||
{{ range .Links }} | ||
<li><a href="{{ .Address }}">{{.Text}}</a>{{if .Description}}: {{.Description}}{{end}}</li> | ||
{{ end }} | ||
</ul> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |