How to wrap response


#1

I want to change response without manually handle in each route handler (i.e: getUser)
How to do that

func config() {
	server.Use(resultWrapper)
	server.GET("/user/:id", getUser)
}

func getUser(c echo.Context) error {
	return c.JSONPretty(http.StatusOK, user{foo: "bar"}, "\t")
}

func resultWrapper(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		c.Response().Before(func() {
			originalResponse := fmt.Sprintf("%v", c.Response().Writer)
			newResponse := myResponse{isSuccess: true, result: originalResponse}
			// how can i do this without dead loop (JSONPretty --> JSONBlob --> Blob --> WriteHeader --> JSONPretty --> ...)
			c.JSONPretty(200, newResponse, "\t") 
		})
		return next(c)
	}
}
````