Skip to content

Commit e453bd7

Browse files
authored
Merge pull request #51 from prometheus/superq/landing_page
Exporter Landing page
2 parents 2ee7ad3 + 00162f9 commit e453bd7

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

web/landing_page.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
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;
3+
margin: 0;
4+
}
5+
nav {
6+
background-color: {{.HeaderColor}};
7+
color: #fff;
8+
font-size: 2rem;
9+
padding: 1rem;
10+
}
11+
.main {
12+
padding: 1rem;
13+
}

web/landing_page.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2023 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
//go:build !genassets
15+
// +build !genassets
16+
17+
//go:generate go run -tags genassets gen_assets.go
18+
19+
package web
20+
21+
import (
22+
"bytes"
23+
_ "embed"
24+
"net/http"
25+
"text/template"
26+
)
27+
28+
// Config represents the configuration of the web listener.
29+
type LandingConfig struct {
30+
HeaderColor string // Used for the landing page header.
31+
CSS string // CSS style tag for the landing page.
32+
Name string // The name of the exporter, generally suffixed by _exporter.
33+
Description string // A short description about the exporter.
34+
Links []LandingLinks // Links displayed on the landing page.
35+
Version string // The version displayed.
36+
}
37+
38+
type LandingLinks struct {
39+
Address string // The URL the link points to.
40+
Text string // The text of the link.
41+
Description string // A descriptive textfor the link.
42+
}
43+
44+
type LandingPageHandler struct {
45+
landingPage []byte
46+
}
47+
48+
var (
49+
//go:embed landing_page.html
50+
landingPagehtmlContent string
51+
//go:embed landing_page.css
52+
landingPagecssContent string
53+
)
54+
55+
func NewLandingPage(c LandingConfig) (*LandingPageHandler, error) {
56+
var buf bytes.Buffer
57+
if c.CSS == "" {
58+
if c.HeaderColor == "" {
59+
// Default to Prometheus orange.
60+
c.HeaderColor = "#e6522c"
61+
}
62+
cssTemplate := template.Must(template.New("landing css").Parse(landingPagecssContent))
63+
if err := cssTemplate.Execute(&buf, c); err != nil {
64+
return nil, err
65+
}
66+
c.CSS = buf.String()
67+
}
68+
t := template.Must(template.New("landing page").Parse(landingPagehtmlContent))
69+
70+
buf.Reset()
71+
if err := t.Execute(&buf, c); err != nil {
72+
return nil, err
73+
}
74+
75+
return &LandingPageHandler{
76+
landingPage: buf.Bytes(),
77+
}, nil
78+
}
79+
80+
func (h *LandingPageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
81+
w.Write(h.landingPage)
82+
}

web/landing_page.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>{{.Name}}</title>
6+
<style>{{.CSS}}</style>
7+
</head>
8+
<body>
9+
<nav>{{.Name}}</nav>
10+
<div class="main">
11+
{{if .Description}}<h3>{{.Description}}</h3>{{end}}
12+
{{if .Version}}<div>Version: {{.Version}}</div>{{end}}
13+
<div><ul>
14+
{{ range .Links }}
15+
<li><a href="{{ .Address }}">{{.Text}}</a>{{if .Description}}: {{.Description}}{{end}}</li>
16+
{{ end }}
17+
</ul>
18+
</div>
19+
</div>
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)