Git Product home page Git Product logo

ginrequestid's Introduction

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/seansa/ginrequestid

Usage

Import it and use it in your router:

import (
    ...
    "github.com/gin-gonic/gin"
    "github.com/seansa/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 ([email protected])

License

GNU GPL v3. See LICENSE file.

ginrequestid's People

Contributors

atarantini avatar seansa avatar

Watchers

James Cloos avatar

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.