requestid

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2024 License: MIT Imports: 2 Imported by: 160

README

RequestID

Run Tests codecov Go Report Card GoDoc

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.

Usage

Start using it

Download and install it.

go get github.com/gin-contrib/requestid

Import it in your code, then use it:

import "github.com/gin-contrib/requestid"

Config

define your custom generator function:

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

package main

import (
  "fmt"
  "net/http"
  "time"

  "github.com/gin-contrib/requestid"
  "github.com/gin-gonic/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:

// Example / request.
r.GET("/", func(c *gin.Context) {
  c.String(http.StatusOK, "id:"+requestid.Get(c))
})

You can also get the request identifier from response header:

> curl -i "http://127.0.0.1:8080"

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
X-Request-ID: 77966910-3912-4193-9b74-267491c51700
Content-Length: 39

id:77966910-3912-4193-9b74-267491c51700

When http request with custom identifier, gin server return the custom identifier in response header.

> curl -i -H "X-Request-ID:test" "http://127.0.0.1:8080"

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
X-Request-Id: 1688221042
Content-Length: 13

id:test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(c *gin.Context) string

Get returns the request identifier

func New

func New(opts ...Option) gin.HandlerFunc

New initializes the RequestID middleware.

Types

type Generator added in v0.0.3

type Generator func() string

type Handler added in v0.0.5

type Handler func(c *gin.Context, requestID string)

type HeaderStrKey added in v0.0.4

type HeaderStrKey string

type Option added in v0.0.3

type Option func(*config)

Option for queue system

func WithCustomHeaderStrKey added in v0.0.4

func WithCustomHeaderStrKey(s HeaderStrKey) Option

WithCustomHeaderStrKey set custom header key for request id

func WithGenerator added in v0.0.3

func WithGenerator(g Generator) Option

WithGenerator set generator function

func WithHandler added in v0.0.5

func WithHandler(handler Handler) Option

WithHandler set handler function for request id with context

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL