...

Text file src/github.com/gin-contrib/requestid/README.md

Documentation: github.com/gin-contrib/requestid

     1# RequestID
     2
     3[![Run Tests](https://github.com/gin-contrib/requestid/actions/workflows/go.yml/badge.svg?branch=master)](https://github.com/gin-contrib/requestid/actions/workflows/go.yml)
     4[![codecov](https://codecov.io/gh/gin-contrib/requestid/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/requestid)
     5[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/requestid)](https://goreportcard.com/report/github.com/gin-contrib/requestid)
     6[![GoDoc](https://godoc.org/github.com/gin-contrib/requestid?status.svg)](https://godoc.org/github.com/gin-contrib/requestid)
     7[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin)
     8
     9Request 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.
    10
    11## Config
    12
    13define your custom generator function:
    14
    15```go
    16func main() {
    17
    18  r := gin.New()
    19
    20  r.Use(
    21    requestid.New(
    22      requestid.WithGenerator(func() string {
    23        return "test"
    24      }),
    25      requestid.WithCustomHeaderStrKey("your-customer-key"),
    26    ),
    27  )
    28
    29  // Example ping request.
    30  r.GET("/ping", func(c *gin.Context) {
    31    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
    32  })
    33
    34  // Listen and Server in 0.0.0.0:8080
    35  r.Run(":8080")
    36}
    37```
    38
    39## Example
    40
    41```go
    42package main
    43
    44import (
    45  "fmt"
    46  "net/http"
    47  "time"
    48
    49  "github.com/gin-contrib/requestid"
    50  "github.com/gin-gonic/gin"
    51)
    52
    53func main() {
    54
    55  r := gin.New()
    56
    57  r.Use(requestid.New())
    58
    59  // Example ping request.
    60  r.GET("/ping", func(c *gin.Context) {
    61    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
    62  })
    63
    64  // Listen and Server in 0.0.0.0:8080
    65  r.Run(":8080")
    66}
    67```
    68
    69How to get the request identifier:
    70
    71```go
    72// Example / request.
    73r.GET("/", func(c *gin.Context) {
    74  c.String(http.StatusOK, "id:"+requestid.Get(c))
    75})
    76```

View as plain text