go-gin

How to log response body in gin

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 05:26:01
问题 I need to log the response body in a middleware of gin, but I don't find how to get the response body. Can anyone help? I am using a middleware like this: func Logger() gin.HandlerFunc { return func(c *gin.Context) { c.Next() statusCode := c.Writer.Status() if statusCode >= 400 { //ok this is an request with error, let's make a record for it //log body here } } } My question is, how to get response body from Context in middleware? 回答1: You need to intercept writing of response and store it

Golang gin gonic web framework proxy route to another backend

风格不统一 提交于 2019-12-08 02:41:30
问题 How to reverse proxy web requests for a few routes to another backend in Gin Gonic web golang framework Is there a way to directly forward in the Handle function as shown below? router := gin.New() router.Handle("POST", "/api/v1/endpoint1", ForwardToAnotherBackend) 回答1: You can do this with the standard library httputil.ReverseProxy. I haven't found a reason to use gin myself yet, I'm a fan of sticking to stdlib whenever possible. However I believe you can wrap this ReverseProxy handler in

Setting up Route Not Found in Gin

若如初见. 提交于 2019-12-06 18:29:51
问题 I've setup a default router and some routes in Gin: router := gin.Default() router.POST("/users", save) router.GET("/users",getAll) but how do I handle 404 Route Not Found in Gin? Originally, I was using httprouter which I understand Gin uses so this was what I originally had... router.NotFound = http.HandlerFunc(customNotFound) and the function: func customNotFound(w http.ResponseWriter, r *http.Request) { //return JSON return } but this won't work with Gin. I need to be able to return JSON

Golang gin gonic web framework proxy route to another backend

淺唱寂寞╮ 提交于 2019-12-06 08:18:28
How to reverse proxy web requests for a few routes to another backend in Gin Gonic web golang framework Is there a way to directly forward in the Handle function as shown below? router := gin.New() router.Handle("POST", "/api/v1/endpoint1", ForwardToAnotherBackend) You can do this with the standard library httputil.ReverseProxy . I haven't found a reason to use gin myself yet, I'm a fan of sticking to stdlib whenever possible. However I believe you can wrap this ReverseProxy handler in gin.WrapH() to be able to use it with your gin router. This was the solution I used for reverse-proxying a

Listen on TCP4 not TCP6

瘦欲@ 提交于 2019-12-06 05:22:28
问题 I am using https://github.com/gin-gonic/gin to write an http service But when I deploy it, it keeps deploying on tcp6(according to netstat) r := gin.Default() //none of these are working , It keeps being listed on tcp6 r.Run(":8080") r.Run("*:8080") r.Run("0.0.0.0:8080") 回答1: The documentation states Run attaches the router to a http.Server and starts listening and serving HTTP requests. It is a shortcut for http.ListenAndServe(addr, router) You can start the server directly using an http

Setting up Route Not Found in Gin

余生长醉 提交于 2019-12-05 01:17:44
I've setup a default router and some routes in Gin: router := gin.Default() router.POST("/users", save) router.GET("/users",getAll) but how do I handle 404 Route Not Found in Gin? Originally, I was using httprouter which I understand Gin uses so this was what I originally had... router.NotFound = http.HandlerFunc(customNotFound) and the function: func customNotFound(w http.ResponseWriter, r *http.Request) { //return JSON return } but this won't work with Gin. I need to be able to return JSON using the c *gin.Context so that I can use: c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message":

Listen on TCP4 not TCP6

你离开我真会死。 提交于 2019-12-04 12:16:09
I am using https://github.com/gin-gonic/gin to write an http service But when I deploy it, it keeps deploying on tcp6(according to netstat) r := gin.Default() //none of these are working , It keeps being listed on tcp6 r.Run(":8080") r.Run("*:8080") r.Run("0.0.0.0:8080") The documentation states Run attaches the router to a http.Server and starts listening and serving HTTP requests. It is a shortcut for http.ListenAndServe(addr, router) You can start the server directly using an http.Server the same way the http package does in ListenAndServe server := &http.Server{Handler: r} l, err := net

Go gin framework CORS

醉酒当歌 提交于 2019-11-29 02:57:31
问题 I'm using Go gin framework gin func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set("Content-Type", "application/json") c.Writer.Header().Set("Access-Control-Allow-Origin", "*") c.Writer.Header().Set("Access-Control-Max-Age", "86400") c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token,