Documentation
¶
Overview ¶
Package xrequestid implements an http middleware for Negroni that assigns a random id to each request
Example:
package main
import (
"fmt"
"net/http"
"github.com/codegangsta/negroni"
"github.com/pilu/xrequestid"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "X-Request-Id is `%s`", r.Header.Get("X-Request-Id"))
})
n := negroni.New()
n.Use(xrequestid.New(16))
n.UseHandler(mux)
n.Run(":3000")
}
Index ¶
Constants ¶
View Source
const DefaultHeaderKey = "X-Request-Id"
By default the middleware set the generated random string to this key in the request header
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GenerateFunc ¶
GenerateFunc is the func used by the middleware to generates the random string.
type XRequestID ¶
type XRequestID struct {
// Size specifies the length of the random length. The length of the result string is twice of n.
Size int
// Generate is a GenerateFunc that generates the random string. The default one uses crypto/rand
Generate GenerateFunc
// HeaderKey is the header name where the middleware set the random string. By default it uses the DefaultHeaderKey constant value
HeaderKey string
}
XRequestID is a middleware that adds a random ID to the request X-Request-Id header
func New ¶
func New(n int) *XRequestID
New returns a new XRequestID middleware instance. n specifies the length of the random length. The length of the result string is twice of n.
func (*XRequestID) ServeHTTP ¶
func (m *XRequestID) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
Click to show internal directories.
Click to hide internal directories.