xrequestid

package module
v0.0.0-...-870344e Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2016 License: MIT Imports: 3 Imported by: 12

README

xrequestid

Package xrequestid implements an http middleware for Negroni that assigns a random id to each request. It's written in the Go programming language.

Docs at http://godoc.org/github.com/pilu/xrequestid

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

type GenerateFunc func(int) (string, error)

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)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL