Documentation
¶
Overview ¶
Package stringid provides string based ID generators, and accompanying middleware for assigning a request ID to standard HTTP request contexts.
Index ¶
- func FromContext(ctxt context.Context) string
- func FromRequest(req *http.Request) string
- func Generate() string
- func HeaderMiddleware(headers ...string) func(http.Handler) http.Handler
- func Middleware(opts ...MiddlewareOption) func(http.Handler) http.Handler
- func WithID(ctxt context.Context, id string) context.Context
- func WithRequest(req *http.Request, id string) *http.Request
- type Generator
- type GeneratorFunc
- type GeneratorMiddleware
- type MiddlewareOption
- type PushGenerator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext returns the ID previously set on the context.
func FromRequest ¶
FromRequest returns the ID previously set on the request's context.
func HeaderMiddleware ¶
HeaderMiddleware creates a standard HTTP middleware handler that sets the request ID as read from one of the supplied headers.
Note: this should be used in a middleware chain only *AFTER* verifying the request header can be trusted.
func Middleware ¶
func Middleware(opts ...MiddlewareOption) func(http.Handler) http.Handler
Middleware creates a standard HTTP middleware handler that sets a request ID on the HTTP context.
If no options are supplied, then a new push-style generator will be used.
By default, the Prefix will be set as "<hostname>/".
Types ¶
type Generator ¶
type Generator interface {
// Generate generates a string ID.
Generate() string
}
Generator is the common interface for ID generators.
var DefaultGenerator Generator
DefaultGenerator is the default ID generator.
func NewUUIDGenerator ¶
func NewUUIDGenerator() Generator
NewUUIDGenerator creates a ID generator that generates UUIDv4 style IDs.
type GeneratorFunc ¶
type GeneratorFunc func() string
GeneratorFunc wraps a standard func as a ID generator.
func (GeneratorFunc) Generate ¶
func (f GeneratorFunc) Generate() string
Generate satisfies the Generator interface.
type GeneratorMiddleware ¶
GeneratorMiddleware provides standard HTTP middleware that sets a HTTP request ID based on the Prefix and a Generator's generated ID.
Useful for logging / tracing purposes.
type MiddlewareOption ¶
type MiddlewareOption func(*GeneratorMiddleware)
MiddlewareOption is a middleware option.
func WithGenerator ¶
func WithGenerator(generator Generator) MiddlewareOption
WithGenerator is a middleware option to set the generator used.
func WithPrefix ¶
func WithPrefix(prefix string) MiddlewareOption
WithPrefix is a middleware option to set a prefix on generated IDs.
type PushGenerator ¶
type PushGenerator struct {
// contains filtered or unexported fields
}
PushGenerator is a push-style ID generator that satisifies the Generator interface.
func NewPushGenerator ¶
func NewPushGenerator(r *rand.Rand) *PushGenerator
NewPushGenerator creates a new push ID generator.
func (*PushGenerator) Generate ¶
func (pg *PushGenerator) Generate() string
Generate generates a unique, 20-character push-style ID.
func (*PushGenerator) String ¶
func (pg *PushGenerator) String() string
String satisfies the fmt.Stringer interface.
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
goji
command
examples/goji/main.go
|
examples/goji/main.go |
|
grpc
command
|
|
|
logrus
command
examples/logrus/main.go
|
examples/logrus/main.go |
|
uuid
command
examples/uuid/main.go
|
examples/uuid/main.go |
|
Package grpcid provides a gRPC middleware interceptor that adds a string ID to the request context.
|
Package grpcid provides a gRPC middleware interceptor that adds a string ID to the request context. |