Documentation ¶
Overview ¶
Package requestid implements an http handler that assigns a randomly generated id to each request.
Example:
package main import ( "fmt" "net/http" "github.com/syntaqx/echo-middleware/requestid" ) func main() { mux := http.NewServeMux() rid := requestid.New() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s is %s", rid.HeaderKey, r.Header.Get(rid.HeaderKey)) }) http.ListenAndServe(":8080", rid.Handler(mux)) }
Index ¶
- Constants
- type GenerateFunc
- type RequestID
- func (m *RequestID) Handler(h http.Handler) http.Handler
- func (m *RequestID) HandlerFunc(w http.ResponseWriter, r *http.Request)
- func (m *RequestID) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
- func (m *RequestID) SetGenerate(f GenerateFunc)
- func (m *RequestID) SetHeaderKey(key string)
Constants ¶
const XRequestID = "X-Request-Id"
XRequestID is the default header the request id is set to.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GenerateFunc ¶
GenerateFunc is the func used by the handler to generate the random id.
type RequestID ¶
type RequestID struct { Generate GenerateFunc HeaderKey string }
RequestID is an http handler that generates an id and assigns it to a request header.
func (*RequestID) Handler ¶
Handler implements the http.Handler interface and can be registered to serve a particular path or subtree in a HTTP server.
func (*RequestID) HandlerFunc ¶
func (m *RequestID) HandlerFunc(w http.ResponseWriter, r *http.Request)
HandlerFunc provides a Martini compatible handler.
func (*RequestID) ServeHTTP ¶
func (m *RequestID) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
ServeHTTP provides a Negroni compatible handler.
func (*RequestID) SetGenerate ¶
func (m *RequestID) SetGenerate(f GenerateFunc)
SetGenerate sets the generate function the request id will be generated from.
func (*RequestID) SetHeaderKey ¶
SetHeaderKey sets the header key the request id will be assigned to.