Skip to content

fino-digital/requestid

This branch is up to date with Wr4thon/requestid:master.

Folders and files

NameName
Last commit message
Last commit date
Nov 5, 2020
Nov 5, 2020
Nov 6, 2020
Nov 16, 2020
Nov 5, 2020
Nov 5, 2020
Nov 27, 2020
Nov 5, 2020
Nov 16, 2020
Nov 16, 2020
Nov 5, 2020

Repository files navigation

requestid

Description

this repository is a collection of middlewares, used to get a requestID from an http request, and set it in rest clients used to perform requests.

supported frameworks

Framework Support Version
Resty v2
Echo v4
Atreugo v10

how to install

go get github.com/Wr4thon/requestid@v0.2.0

How to use

resty

package main

import (
	"context"

	"github.com/Wr4thon/requestid"
	"github.com/go-resty/resty/v2"
)

func main(){
	client := resty.
		New().
		OnBeforeRequest(requestid.RestyMiddleware)

	// this most likely will be set by an http framework such as atreugo or echo.
	ctx := context.TODO()
	ctx = requestid.Set(ctx, "<TheRequestID>")

	resp, err := client.R().
		SetContext(ctx).
		Get("https://example.com")

	if err != nil {
		panic(err)
	}

	println(string(resp.Body()))
}

echo

package main

import (
	"net/http"

	"github.com/Wr4thon/requestid"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

func main() {
	e := echo.New()
	e.Use(middleware.RequestID())
	e.Use(requestid.EchoMiddleware())

	e.GET("/", func(c echo.Context) error {
		rid, err := requestid.Get(c.Request().Context())

		if err != nil {
      println(err.Error())
			return c.NoContent(http.StatusInternalServerError)
		}

		println(rid)
		return c.NoContent(http.StatusNoContent)
	})

	e.Logger.Error(e.Start(":1337"))
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%