/requestid

RequestID middleware for Go

Primary LanguageGoMIT LicenseMIT

requestid GoDocGo Report Card

RequestID middleware for Go. RequestID adds a UUID as X-Request-ID header if not already set. It also adds it to the http.Request Context. Use requestid.FromContext to get the generated request id.

Example

package main

import (
	"fmt"
	"html"
	"log"
	"net/http"

	"github.com/ascarter/requestid"
)

func handler(w http.ResponseWriter, r *http.Request) {
	rid, _ := requestid.FromContext(r.Context())
	log.Println("Running hello handler:", rid)
	fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}

func main() {
	h := http.HandlerFunc(handler)
	http.Handle("/", requestid.RequestIDHandler(h))
	log.Fatal(http.ListenAndServe(":8080", nil))
}