wrequestid

package module
v0.0.0-...-ed64bbc Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: MIT Imports: 4 Imported by: 0

README

WRequestID

GitHub Repo stars GitHub GitHub go.mod Go version GitHub CI Status Go Report Card Go.Dev reference codecov

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.

Example

package main

import (
	"bytes"
	"github.com/gin-gonic/gin"
	"github.com/wyy-go/wrequestid"
	"log"
	"net/http"
)

func main() {
	r := gin.New()

	r.Use(wrequestid.New())

	r.GET("/", func(c *gin.Context) {
		var buf bytes.Buffer
		buf.WriteString("GetRequestID:\t" + wrequestid.GetRequestID(c))
		buf.WriteString("\n")
		buf.WriteString("FromRequestID:\t" + wrequestid.FromRequestID(c.Request.Context()))
		c.String(http.StatusOK, buf.String())
	})

	// Listen and Server in 0.0.0.0:8080
	if err := r.Run(":8080"); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromRequestID

func FromRequestID(ctx context.Context) string

func GetRequestID

func GetRequestID(c *gin.Context) string

GetRequestID returns the request identifier

func New

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

New initializes the RequestID middleware.

Types

type Option

type Option func(*Options)

func WithGenerator

func WithGenerator(gen func() string) Option

func WithRequestIDHeader

func WithRequestIDHeader(header string) Option

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options 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