first commit
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
# RequestID
|
||||
|
||||
Request ID middleware for Gin Framework. Adds an indentifier to the response using the `X-Request-ID` header. Passes the `X-Request-ID` value back to the caller if it's sent in the request headers.
|
||||
|
||||
Copied from [gin-contrib/requestid](https://github.com/gin-contrib/requestid)
|
||||
|
||||
## Config
|
||||
|
||||
define your custom generator function:
|
||||
|
||||
```go
|
||||
func main() {
|
||||
|
||||
r := gin.New()
|
||||
|
||||
r.Use(
|
||||
requestid.New(
|
||||
requestid.WithGenerator(func() string {
|
||||
return "test"
|
||||
}),
|
||||
requestid.WithCustomHeaderStrKey("your-customer-key"),
|
||||
),
|
||||
)
|
||||
|
||||
// Example ping request.
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitverse.ru/andoma/gin-contrib/requestid"
|
||||
"gitverse.ru/andoma/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
r := gin.New()
|
||||
|
||||
r.Use(requestid.New())
|
||||
|
||||
// Example ping request.
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
How to get the request identifier:
|
||||
|
||||
```go
|
||||
// Example / request.
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "id:"+requestid.Get(c))
|
||||
})
|
||||
```
|
||||
Reference in New Issue
Block a user