...

Source file src/github.com/gin-contrib/requestid/_example/example02/main.go

Documentation: github.com/gin-contrib/requestid/_example/example02

     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  	// Example ping request.
    29  	r.GET("/ping", func(c *gin.Context) {
    30  		c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
    31  	})
    32  
    33  	// Example / request.
    34  	r.GET("/", func(c *gin.Context) {
    35  		c.String(http.StatusOK, "id:"+requestid.Get(c))
    36  	})
    37  
    38  	// Listen and Server in 0.0.0.0:8080
    39  	if err := r.Run(":8080"); err != nil {
    40  		log.Fatal(err)
    41  	}
    42  }
    43  

View as plain text