You can set ContextWithFallback field to true after initializing default Gin Engine.
r := gin.Default()
r.ContextWithFallback = true
This will tell gin to extract value using gin.Context.Value()
when value is not found using gin.Context.Request.Context.Value()
. This way you don't need to modify your getCurrentUser function call as suggested by @mkopriva
This code here checks if gin.Engine.ContextWithFallback
is enabled and gin.Request.Context()
is not nil
. If both the conditions are met then gin.Context.Value(key)
returns gin.Context.Request.Context.Value(key)
.