metrics.go 737 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package app
  5. import (
  6. "net/http"
  7. "gopkg.in/macaron.v1"
  8. "gogs.io/gogs/internal/authutil"
  9. "gogs.io/gogs/internal/conf"
  10. )
  11. func MetricsFilter() macaron.Handler {
  12. return func(w http.ResponseWriter, r *http.Request) {
  13. if !conf.Prometheus.Enabled {
  14. w.WriteHeader(http.StatusNotFound)
  15. return
  16. }
  17. if !conf.Prometheus.EnableBasicAuth {
  18. return
  19. }
  20. username, password := authutil.DecodeBasic(r.Header)
  21. if username != conf.Prometheus.BasicAuthUsername || password != conf.Prometheus.BasicAuthPassword {
  22. w.WriteHeader(http.StatusForbidden)
  23. return
  24. }
  25. }
  26. }