requestcontext

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: Apache-2.0, MIT Imports: 2 Imported by: 2

README

go-requestcontext

This repo provides a small shim for tagging a request ID onto the request context, and then get access to that request ID in sub-contexts.

The impetus for this code is that the setting and use of the request ID context key may be in different projects, and it may be desirable to decouple the dependency between such projects. Since context keys are advisted to be set using a custom sentinal type, this repo provides the place for that singleton value to live.

Usage

import (
    "context"
    "net/http"

    requestcontext "github.com/willscott/go-requestcontext"
)

func main() {
    // Injection
    mux := http.NewServeMux()
    handler := requestcontext.Middleware(mux, "X-RequestID")
    ...
}

func sendSubrequest(ctx context.Context) {
    rid := requestcontext.IDFromContext(ctx)
    ...
}

Documentation

See godoc for API documentation.

Contributing

Contributions are welcome! This repository is part of the IPFS project and therefore governed by our contributing guidelines.

License

SPDX-License-Identifier: Apache-2.0 OR MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IDFromContext

func IDFromContext(ctx context.Context) string

func Middleware

func Middleware(handler http.Handler, key string) http.Handler

Types

This section is empty.

Jump to

Keyboard shortcuts

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