func backend()

in content/intermediate/245_x-ray/sample-front.files/main.go [60:82]


func backend(r *http.Request, slow string) (string, error) {
	resp, err := ctxhttp.Get(
		r.Context(),
		xray.Client(&http.Client{Transport: tr}),
		fmt.Sprintf("http://x-ray-sample-back-k8s.default.svc.cluster.local?slow=%s", slow),
	)

	if err != nil {
		return "", err
	}

	defer resp.Body.Close()

	if resp.StatusCode == http.StatusOK {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return "", err
		}
		return string(body), nil
	} else {
		return "", errors.New(fmt.Sprintf("Bad backend request - http status: %s", strconv.Itoa(resp.StatusCode)))
	}
}