handler.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2021 by the contributors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package internalserver
  15. import (
  16. "fmt"
  17. "net/http"
  18. "net/http/pprof"
  19. "sort"
  20. "github.com/metalmatze/signal/healthcheck"
  21. "github.com/prometheus/client_golang/prometheus"
  22. "github.com/prometheus/client_golang/prometheus/promhttp"
  23. )
  24. // Handler is a http.ServeMux that knows about all endpoints to render a nice index page.
  25. type Handler struct {
  26. http.ServeMux
  27. endpoints []endpoint
  28. name string
  29. }
  30. // endpoint has a description to a pattern.
  31. type endpoint struct {
  32. Pattern string
  33. Description string
  34. }
  35. // NewHandler creates a new internalserver Handler.
  36. func NewHandler(options ...Option) *Handler {
  37. h := &Handler{name: "Internal"}
  38. for _, option := range options {
  39. option(h)
  40. }
  41. h.HandleFunc("/", h.index)
  42. return h
  43. }
  44. // AddEndpoint wraps HandleFunc for adding http handlers to add a meaningful description to the index page.
  45. func (h *Handler) AddEndpoint(pattern string, description string, handler http.HandlerFunc) {
  46. h.endpoints = append(h.endpoints, endpoint{
  47. Pattern: pattern,
  48. Description: description,
  49. })
  50. // Sort endpoints by pattern after adding a new one, to always show them in the same order.
  51. sort.Slice(h.endpoints, func(i, j int) bool {
  52. return h.endpoints[i].Pattern < h.endpoints[j].Pattern
  53. })
  54. h.HandleFunc(pattern, handler)
  55. }
  56. func (h *Handler) index(w http.ResponseWriter, r *http.Request) {
  57. html := fmt.Sprintf("<html><head><title>%s</title></head><body>\n", h.name)
  58. html += fmt.Sprintf("<h1>%s</h1>\n", h.name)
  59. for _, e := range h.endpoints {
  60. html += fmt.Sprintf("<p><a href='%s'>%s - %s</a></p>\n", e.Pattern, e.Pattern, e.Description)
  61. }
  62. html += `</body></html>`
  63. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  64. _, _ = w.Write([]byte(html))
  65. }
  66. // Option is a func that modifies the configuration for the internalserver handler.
  67. type Option func(h *Handler)
  68. // WithName allows to set an application name for the internalserver handler.
  69. func WithName(name string) Option {
  70. return func(h *Handler) {
  71. h.name = name
  72. }
  73. }
  74. // WithHealthchecks adds the healthchecks endpoints /live and /ready to the internalserver.
  75. func WithHealthchecks(healthchecks healthcheck.Handler) Option {
  76. return func(h *Handler) {
  77. h.AddEndpoint(
  78. "/live",
  79. "Exposes liveness checks",
  80. healthchecks.LiveEndpoint,
  81. )
  82. h.AddEndpoint(
  83. "/ready",
  84. "Exposes readiness checks",
  85. healthchecks.ReadyEndpoint,
  86. )
  87. }
  88. }
  89. // WithPrometheusRegistry adds a /metrics endpoint to the internalserver.
  90. func WithPrometheusRegistry(registry *prometheus.Registry) Option {
  91. return func(h *Handler) {
  92. h.AddEndpoint(
  93. "/metrics",
  94. "Exposes Prometheus metrics",
  95. promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP,
  96. )
  97. }
  98. }
  99. // WithPProf adds all pprof endpoints under /debug to the internalserver.
  100. func WithPProf() Option {
  101. return func(h *Handler) {
  102. m := http.NewServeMux()
  103. m.HandleFunc("/debug/pprof/", pprof.Index)
  104. m.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
  105. m.HandleFunc("/debug/pprof/profile", pprof.Profile)
  106. m.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
  107. m.HandleFunc("/debug/pprof/trace", pprof.Trace)
  108. m.HandleFunc("/debug/", func(w http.ResponseWriter, r *http.Request) {
  109. http.Redirect(w, r, "/debug/pprof/", http.StatusMovedPermanently)
  110. })
  111. h.AddEndpoint(
  112. "/debug/",
  113. "Exposes pprof endpoints to consume via HTTP",
  114. m.ServeHTTP,
  115. )
  116. }
  117. }