Git Product home page Git Product logo

requestid's Introduction

RequestID

Run Tests codecov Go Report Card GoDoc Join the chat at https://gitter.im/gin-gonic/gin

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

requestid's People

Contributors

appleboy avatar bernardoow avatar dependabot[bot] avatar pscheid92 avatar quantifex avatar rfyiamcool avatar someone-stole-my-name avatar thinkerou avatar wwwmmxxx avatar zacscoding avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

requestid's Issues

RequestId on Request Header

Hi,

Was trying this lib, and notice the Request ID is only set on the response headers and not on the request.
I want to get the request id from the gin context for logging and other purposes.

How about setting the request header in both request and response objects?

Nil pointer when Get RequestID

Hi,

i met an error when i used requestid.Get(*gin.Context) method, which would panic with nil pointer

since i used c.Copy() ( gin.Context.Copy() ) in my code, the c.Writer ( gin.Context.Writer ) became as an nil pointer.

Putting the Request ID as Request Header

Hi,

Currently the request ID is stored in and retrieved from the gin context.

I wanted to add the request ID to the gin logging output. The problem is that gin logger currently accepts a LogFormatterParams struct with the Request object and not the Context object, so even if I provide a custom LogFormatter, I cannot log the request ID that way.

Can you add an option to add the request ID as a header to the request, or to the context.Keys map?

v0.0.4 tag moved

the head for v0.0.4 seems to have moved at least once in the lifespan of the project; the caching server for fly.io has a different go.sum checksum

verifying github.com/gin-contrib/[email protected]: checksum mismatch
   downloaded: h1:XKTeDRVmaVk6mdZrLKujeK+Q3GXWPgrbYu+2akE5XMQ=
   go.sum:     h1:jZhoc2L8MMnxZBgV6c6BD9Ly4/k8TGa1PwnH2GuvVoA=

RequestID should be return when request exist

The code

return func(c *gin.Context) {
	// Get id from request
	rid := c.GetHeader(headerXRequestID)

	if rid == "" {
		rid = cfg.Generator()
	}

	// Return the X-Request-ID in in any case
	c.Header(headerXRequestID, rid)

	c.Next()
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.