Skip to content

btubbs/ginrequestid

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Gingonic X-Request-Id middleware

Basic middleware to add and expose an UUID4 for each request based on the code posted on Dan Sosedoff development and experiments website.

It will set the variable RequestId or order to use it from within the application for logging or propagation.

If X-Request-Id header is sent in the request, it will set that value as RequestId.

Installation

Use go get to fetch the package:

$ go get github.com/atarantini/ginrequestid

Usage

Import it and use it in your router:

import (
    ...
    "github.com/gin-gonic/gin"
    "github.com/atarantini/ginrequestid"
    ...
)

func main() {
    // Initialize router
    r := gin.Default()

    // Load middleware
    r.Use(ginrequestid.RequestId())

    // Your routes
    r.GET("/ping", func(c *gin.Context) {
        c.String(200, "pong")
    })

    // Start server
    r.Run()
}

Check if it's working:

$ curl --verbose localhost:8080/ping
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /ping HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< X-Request-Id: 3528d29d-f5c6-4758-ae05-eee5a56c27ea
< Date: Wed, 25 Jan 2017 12:42:30 GMT
< Content-Length: 4
<
* Connection #0 to host localhost left intact

Use it for logging::

r.GET("/ping", func(c *gin.Context) {
    fmt.Println(fmt.Sprintf("[request-id:%s][event:ping]", c.MustGet("RequestId")))
    c.String(200, "pong")
})

Changelog

0.0.2 - 2018-03-06

  • Fix for change in signature in the main dependecy of the package. In order to use the last version of github.com/satori/go.uuid update to 0.0.2 version.

0.0.1 - 2017-02-07

  • Initial release, basic middleware. It works!

Author

Andres Tarantini (atarantini@gmail.com)

License

GNU GPL v3. See LICENSE file.

About

Gingonic X-Request-Id middleware

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%