// Copyright 2021 by the contributors. // // 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. package internalserver import ( "fmt" "net/http" "net/http/pprof" "sort" "github.com/metalmatze/signal/healthcheck" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) // Handler is a http.ServeMux that knows about all endpoints to render a nice index page. type Handler struct { http.ServeMux endpoints []endpoint name string } // endpoint has a description to a pattern. type endpoint struct { Pattern string Description string } // NewHandler creates a new internalserver Handler. func NewHandler(options ...Option) *Handler { h := &Handler{name: "Internal"} for _, option := range options { option(h) } h.HandleFunc("/", h.index) return h } // AddEndpoint wraps HandleFunc for adding http handlers to add a meaningful description to the index page. func (h *Handler) AddEndpoint(pattern string, description string, handler http.HandlerFunc) { h.endpoints = append(h.endpoints, endpoint{ Pattern: pattern, Description: description, }) // Sort endpoints by pattern after adding a new one, to always show them in the same order. sort.Slice(h.endpoints, func(i, j int) bool { return h.endpoints[i].Pattern < h.endpoints[j].Pattern }) h.HandleFunc(pattern, handler) } func (h *Handler) index(w http.ResponseWriter, r *http.Request) { html := fmt.Sprintf("