...
1 package main
2
3 import (
4 "fmt"
5 "log"
6 "net/http"
7 "time"
8
9 "github.com/gin-contrib/requestid"
10 "github.com/gin-gonic/gin"
11 )
12
13 func main() {
14 r := gin.New()
15
16 r.Use(
17 requestid.New(
18 requestid.WithGenerator(func() string {
19 return "test"
20 }),
21 requestid.WithCustomHeaderStrKey("your-customer-key"),
22 requestid.WithHandler(func(c *gin.Context, requestID string) {
23 log.Printf("RequestID: %s", requestID)
24 }),
25 ),
26 )
27
28
29 r.GET("/ping", func(c *gin.Context) {
30 c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
31 })
32
33
34 r.GET("/", func(c *gin.Context) {
35 c.String(http.StatusOK, "id:"+requestid.Get(c))
36 })
37
38
39 if err := r.Run(":8080"); err != nil {
40 log.Fatal(err)
41 }
42 }
43
View as plain text