Files
grafana/devenv/docker/blocks/slow_proxy/main.go
T
Marcus Efraimsson 348e76fc8e Datasource: Shared HTTP client provider for core backend data sources and any data source using the data source proxy (#33439)
Uses new httpclient package from grafana-plugin-sdk-go introduced 
via grafana/grafana-plugin-sdk-go#328. 
Replaces the GetHTTPClient, GetTransport, GetTLSConfig methods defined 
on DataSource model.
Longer-term the goal is to migrate core HTTP backend data sources to use the 
SDK contracts and using httpclient.Provider for creating HTTP clients and such.

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
2021-05-19 23:53:41 +02:00

39 lines
819 B
Go

package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"time"
)
func main() {
origin := os.Getenv("ORIGIN_SERVER")
if origin == "" {
origin = "http://localhost:9090/"
}
sleepDurationStr := os.Getenv("SLEEP_DURATION")
if sleepDurationStr == "" {
sleepDurationStr = "60s"
}
sleep, err := time.ParseDuration(sleepDurationStr)
if err != nil {
log.Fatalf("failed to parse SLEEP_DURATION: %v", err)
}
originURL, _ := url.Parse(origin)
proxy := httputil.NewSingleHostReverseProxy(originURL)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("sleeping for %s then proxying request: url '%s', headers: '%v'", sleep.String(), r.RequestURI, r.Header)
<-time.After(sleep)
proxy.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(":3011", nil))
}