aboutsummaryrefslogtreecommitdiff
path: root/internal/app/metrics.go
blob: 45a9b74e9173dd3a80afce600acad1a51f616a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package app

import (
	"net/http"

	"gopkg.in/macaron.v1"

	"gogs.io/gogs/internal/authutil"
	"gogs.io/gogs/internal/conf"
)

func MetricsFilter() macaron.Handler {
	return func(w http.ResponseWriter, r *http.Request) {
		if !conf.Prometheus.Enabled {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		if !conf.Prometheus.EnableBasicAuth {
			return
		}

		username, password := authutil.DecodeBasic(r.Header)
		if username != conf.Prometheus.BasicAuthUsername || password != conf.Prometheus.BasicAuthPassword {
			w.WriteHeader(http.StatusForbidden)
			return
		}
	}
}