New to go/gin and I am trying to understand how to access the gin context in a situation where you are getting a callback from a 3rd party library.
gin.GET("/someurl", someHandler)
...
func someHandler(c *gin.Context) {
// pass request off to third party lib that will call our GetSession() method later
// the third party does not take a gin context, only http request and writer
3rdpartylib.HandleRequest(c.Writer, c.Request)
}
func (t *THINGY) GetSession(w http.ResponseWriter, r *http.Request) {
// How do I get the gin context for the request back?
}
Does gin put a reference to itself in the request object somewhere?
If not, should I stash a reference in the request object myself?
CodePudding user response:
You can pass it in the request context:
newCtx:=context.WithValue(Request.Context(),"session",c)
3rdpartylib.HandleRequest(c.Writer, c.Request.WithContext(newCtx))
...
func (t *THINGY) GetSession(w http.ResponseWriter, r *http.Request) {
c:=r.Context().Value("session").(*gin.Context)
}