- chroma/v2: v2.20.0 → v2.21.1 - go-pkgz/auth/v2: v2.1.0 → v2.1.1 - go-pkgz/rest: v1.20.4 → v1.20.6 - golang.org/x/* packages to latest Also exclude "meaningless package names" revive warning in linter config.
28 lines
681 B
Go
28 lines
681 B
Go
package rest
|
|
|
|
import (
|
|
"expvar"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// Metrics responds to GET /metrics with list of expvar
|
|
func Metrics(onlyIps ...string) func(http.Handler) http.Handler {
|
|
return func(h http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == "GET" && strings.HasSuffix(strings.ToLower(r.URL.Path), "/metrics") {
|
|
if matched, ip, err := matchSourceIP(r, onlyIps); !matched || err != nil {
|
|
_ = EncodeJSON(w, http.StatusForbidden, JSON{"error": fmt.Sprintf("ip %s rejected", ip)})
|
|
return
|
|
}
|
|
expvar.Handler().ServeHTTP(w, r)
|
|
return
|
|
}
|
|
h.ServeHTTP(w, r)
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
}
|