func()

in walkthroughs/howto-timeout-policy/src/colorgateway/main.go [56:80]


func (h *colorHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	color, err, resCode, body := getColorFromColorTeller(request)
	
	if err != nil {
		writer.WriteHeader(http.StatusInternalServerError)
		writer.Write([]byte("500 - Unexpected Error"))
		return
	}
	if resCode != 200 {
        writer.WriteHeader(resCode)
		writer.Write(body)
		return
	}

	colorsMutext.Lock()
	defer colorsMutext.Unlock()

	addColor(color)
	statsJson, err := json.Marshal(getRatios())
	if err != nil {
		fmt.Fprintf(writer, `{"color":"%s", "error":"%s"}`, color, err)
		return
	}
	fmt.Fprintf(writer, `{"color":"%s", "stats": %s}`, color, statsJson)
}