func main()

in content/intermediate/245_x-ray/sample-back.files/main.go [24:54]


func main() {

	http.Handle("/", xray.Handler(xray.NewFixedSegmentNamer(appName), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		slow := param(r, "slow", "false")

		res := &response{Message: "42 - The Answer to the Ultimate Question of Life, The Universe, and Everything.", Random: []int{}}

		count := time.Now().Second()
		gen := random(res)

		_, seg := xray.BeginSubsegment(r.Context(), appName + "-gen")

		for i := 0; i < count; i++ {
			gen()
			if slow == "true" {
				time.Sleep(10 * time.Millisecond)
			}
		}

		seg.Close(nil)

		// Beautify the JSON output - only for display
		out, _ := json.MarshalIndent(res, "", "  ")

		w.Header().Set("Content-Type", "application/json")
		io.WriteString(w, string(out))

	})))
	http.ListenAndServe(":8080", nil)
}