|
| 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 | +} |
0 commit comments