requestid

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: MIT Imports: 2 Imported by: 0

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.

Config

define your custom generator function:

func main() {

	r := gin.New()

	r.Use(requestid.New(requestid.Config{
		Generator: func() string {
			return "test"
		},
	}))

	// 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))
})

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(config ...Config) gin.HandlerFunc

New initializes the RequestID middleware.

Types

type Config

type Config struct {
	// Generator defines a function to generate an ID.
	// Optional. Default: func() string {
	//   return uuid.New().String()
	// }
	Generator func() string
}

Config defines the config for RequestID middleware

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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