From 25ae11ff277c2ac0bdbcb47f355d0b87fee927d6 Mon Sep 17 00:00:00 2001 From: Ingo Oeser Date: Wed, 8 Jul 2015 10:45:28 +0200 Subject: [PATCH] Support basic auth since many API services are behind authenticated endpoints --- expvars.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/expvars.go b/expvars.go index 1dfd5db..5d63bf7 100644 --- a/expvars.go +++ b/expvars.go @@ -4,6 +4,7 @@ import ( "errors" "io" "net/http" + "os" "time" "github.com/antonholmquist/jason" @@ -17,13 +18,22 @@ type Expvar struct { *jason.Object } +func getBasicAuthEnv() (user, password string) { + return os.Getenv("HTTP_USER"), os.Getenv("HTTP_PASSWORD") +} + // FetchExpvar fetches expvar by http for the given addr (host:port) func FetchExpvar(addr string) (*Expvar, error) { e := &Expvar{&jason.Object{}} client := &http.Client{ Timeout: 1 * time.Second, // TODO: make it configurable or left default? } - resp, err := client.Get(addr) + + req, _ := http.NewRequest("GET", addr, nil) + if user, pass := getBasicAuthEnv(); user != "" && pass != "" { + req.SetBasicAuth(user, pass) + } + resp, err := client.Do(req) if err != nil { return e, err }