requestid

package
v0.0.0-...-56ccf34 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2017 License: MIT Imports: 3 Imported by: 0

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

View Source
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

type GenerateFunc func() (string, error)

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 New

func New() *RequestID

New returns a new RequestID handler instance.

func (*RequestID) Handler

func (m *RequestID) Handler(h http.Handler) http.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

func (m *RequestID) SetHeaderKey(key string)

SetHeaderKey sets the header key the request id will be assigned to.

Jump to

Keyboard shortcuts

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