func MakeRouteResponse(w http.ResponseWriter, req *http.Request, resp_body []byte, code int, start time.Time, endpoint_name string) error {
	end := time.Now()
	duration := fmt.Sprintf("%v", end.Sub(start))
	if resp_body != nil && code == http.StatusOK {
		// add the response to the stats
		bbpd_stats.AddResponse(start)

		var b []byte
		var json_err error
		w.Header().Set(bbpd_const.CONTENTTYPE, bbpd_const.JSONMIME)

		// look for the X-BBPD-Verbose and X-BBPD-Indent headers
		verbose_output := false // should output include timing info?
		indent_output := false  // should output be indented/pretty-printed?
		_, verbose_output = req.Header[bbpd_const.X_BBPD_VERBOSE]
		_, indent_output = req.Header[bbpd_const.X_BBPD_INDENT]

		if !verbose_output {
			b = resp_body
		} else {
			b, json_err = json.Marshal(bbpd_msg.Response{
				Name:       endpoint_name,
				StatusCode: code,
				Body:       resp_body,
				Run: bbpd_msg.RunInfo{Method: req.Method,
					Host:     bbpd_const.LOCALHOST,
					Duration: duration,
					Start:    start,
					End:      end}})
			if json_err != nil {
				e := fmt.Sprintf("route_response.MakeRouteResponse:marshal failure %s",
					json_err.Error())
				log.Printf(e)
				http.Error(w, e, http.StatusInternalServerError)
				return json_err
			}
		}

		// we support pretty-printing (indent)
		// just pass indent=1 (the 1 can be anything) in the url
		if indent_output {
			var buf bytes.Buffer
			if i_err := json.Indent(&buf, b, "", "\t"); i_err != nil {
				// could not pretty print!
				e := fmt.Sprintf("route_response.MakeRouteResponse cannot indent %s", string(b))
				log.Printf(e)
				unindented_str := string(b)
				w.Header().Set(bbpd_const.CONTENTLENGTH,
					strconv.Itoa(len(unindented_str)))
				io.WriteString(w, unindented_str)
			} else {
				// do the pretty print
				indented_str := buf.String()
				w.Header().Set(bbpd_const.CONTENTLENGTH,
					strconv.Itoa(len(indented_str)))
				io.WriteString(w, indented_str)
			}
		} else {
			// no pretty print requested
			unindented_str := string(b)
			w.Header().Set(bbpd_const.CONTENTLENGTH,
				strconv.Itoa(len(unindented_str)))
			io.WriteString(w, unindented_str)
		}
		return nil
	} else {
		s := ""
		if resp_body != nil {
			s = string(resp_body)
		}
		e := fmt.Sprintf("route_response.MakeRouteResponse %s", s)
		log.Printf(e)
		http.Error(w, e, http.StatusBadRequest)
		return errors.New(e)
	}
}
// MakeRouteResponse wraps a dynamo response with some debugging information related to http codes and request duration.
MakeRouteResponse wraps a dynamo response with some debugging information related to http codes and request duration.
// MakeRouteResponse wraps a dynamo response with some debugging information related to http codes and request duration .
func MakeRouteResponse(w http.ResponseWriter, req *http.Request, resp_body []byte, code int, start time.Time, endpoint_name string) error {
	end := time.Now()
	duration := fmt.Sprintf("%v", end.Sub(start))
	if resp_body != nil && code == http.StatusOK {
		bbpd_stats.AddResponse(start)
		var b []byte
		var json_err error
		w.Header().Set(bbpd_const.CONTENTTYPE, bbpd_const.JSONMIME)
		verbose_output := false 
		indent_output := false  
		_, verbose_output = req.Header[bbpd_const.X_BBPD_VERBOSE]
		_, indent_output = req.Header[bbpd_const.X_BBPD_INDENT]
		if !verbose_output {
			b = resp_body
		} else {
			b, json_err = json.Marshal(bbpd_msg.Response{
				Name:       endpoint_name,
				StatusCode: code,
				Body:       resp_body,
				Run: bbpd_msg.RunInfo{Method: req.Method,
					Host:     bbpd_const.LOCALHOST,
					Duration: duration,
					Start:    start,
					End:      end}})
			if json_err != nil {
				e := fmt.Sprintf("route_response.MakeRouteResponse:marshal failure %s",
					json_err.Error())
				log.Printf(e)
				http.Error(w, e, http.StatusInternalServerError)
				return json_err
			}
		}
		if indent_output {
			var buf bytes.Buffer
			if i_err := json.Indent(&buf, b, "", "\t"); i_err != nil {
				e := fmt.Sprintf("route_response.MakeRouteResponse cannot indent %s", string(b))
				log.Printf(e)
				unindented_str := string(b)
				w.Header().Set(bbpd_const.CONTENTLENGTH,
					strconv.Itoa(len(unindented_str)))
				io.WriteString(w, unindented_str)
			} else {
				indented_str := buf.String()
				w.Header().Set(bbpd_const.CONTENTLENGTH,
					strconv.Itoa(len(indented_str)))
				io.WriteString(w, indented_str)
			}
		} else {
			unindented_str := string(b)
			w.Header().Set(bbpd_const.CONTENTLENGTH,
				strconv.Itoa(len(unindented_str)))
			io.WriteString(w, unindented_str)
		}
		return nil
	} else {
		s := ""
		if resp_body != nil {
			s = string(resp_body)
		}
		e := fmt.Sprintf("route_response.MakeRouteResponse %s", s)
		log.Printf(e)
		http.Error(w, e, http.StatusBadRequest)
		return errors.New(e)
	}
}
func MakeRouteResponse ( w http . ResponseWriter , req * http . Request , resp_body [ ] byte , code int , start time . Time , endpoint_name string ) error { end := time . Now ( ) duration := fmt . Sprintf ( " " , end . Sub ( start ) ) if resp_body != nil && code == http . StatusOK { bbpd_stats . AddResponse ( start ) var b [ ] byte var json_err error w . Header ( ) . Set ( bbpd_const . CONTENTTYPE , bbpd_const . JSONMIME ) verbose_output := false indent_output := false _ , verbose_output = req . Header [ bbpd_const . X_BBPD_VERBOSE ] _ , indent_output = req . Header [ bbpd_const . X_BBPD_INDENT ] if ! verbose_output { b = resp_body } else { b , json_err = json . Marshal ( bbpd_msg . Response { Name : endpoint_name , StatusCode : code , Body : resp_body , Run : bbpd_msg . RunInfo { Method : req . Method , Host : bbpd_const . LOCALHOST , Duration : duration , Start : start , End : end } } ) if json_err != nil { e := fmt . Sprintf ( " " , json_err . Error ( ) ) log . Printf ( e ) http . Error ( w , e , http . StatusInternalServerError ) return json_err } } if indent_output { var buf bytes . Buffer if i_err := json . Indent ( & buf , b , " " , " \t " ) ; i_err != nil { e := fmt . Sprintf ( " " , string ( b ) ) log . Printf ( e ) unindented_str := string ( b ) w . Header ( ) . Set ( bbpd_const . CONTENTLENGTH , strconv . Itoa ( len ( unindented_str ) ) ) io . WriteString ( w , unindented_str ) } else { indented_str := buf . String ( ) w . Header ( ) . Set ( bbpd_const . CONTENTLENGTH , strconv . Itoa ( len ( indented_str ) ) ) io . WriteString ( w , indented_str ) } } else { unindented_str := string ( b ) w . Header ( ) . Set ( bbpd_const . CONTENTLENGTH , strconv . Itoa ( len ( unindented_str ) ) ) io . WriteString ( w , unindented_str ) } return nil } else { s := " " if resp_body != nil { s = string ( resp_body ) } e := fmt . Sprintf ( " " , s ) log . Printf ( e ) http . Error ( w , e , http . StatusBadRequest ) return errors . New ( e ) } }
MakeRouteResponse wraps a dynamo response with some debugging information related to http codes and request duration.
MakeRouteResponse wraps a dynamo response with some debugging information related to http codes and request duration .
// add the response to the stats // look for the X-BBPD-Verbose and X-BBPD-Indent headers // should output include timing info? // should output be indented/pretty-printed? // we support pretty-printing (indent) // just pass indent=1 (the 1 can be anything) in the url // could not pretty print! // do the pretty print // no pretty print requested