ion: github.com/estebarb/ion/components/reqctx Index | Files

package reqctx

import "github.com/estebarb/ion/components/reqctx"

Package reqctx provides an uniform way to access request context.

For this package a context is a single value that is "carried" by the request. This single value is an interface{}, and is expected that the programs provide its own ContextFactory, and have a corresponding interface that could be used to make safe type assertion.

This can be both in Go 1.7+ or previous versions. If used in Go 1.7+ it stores the context in the Request context, but if used in Go <1.7 then it stores it in a map[*Request] protected by mutexes. The user code don't need to any code change to work in both versions, as long as it uses reqctx in the recommended way.

If used with the Ion Router then you don't have to worry about allocating the context or destroying it after usage.

Index

Package Files

common.go reqctx.go

func BasicContextFactory Uses

func BasicContextFactory() interface{}

BasicContextFactory creates a map[string]interface{} where the request context could be stored.

Note that this one is intended for trivial usage or reqctx testing, and must not be used with the rest of Ion.

In Ion is expected that the application provides its own ContextFactory, or at least uses the default ContextFactory of the router package.

type State Uses

type State struct {
    // contains filtered or unexported fields
}

State is used to manage the requests context

func New Uses

func New(ctxFactory func() interface{}) *State

New creates a new context manager

func NewDefault Uses

func NewDefault() *State

NewDefault returns a new State manager that uses BasicContextFactory

func (*State) Context Uses

func (s *State) Context(r *http.Request) interface{}

Context returns the context of the request

func (*State) DestroyContext Uses

func (s *State) DestroyContext(r *http.Request)

DestroyContext destroys the context. Note that in Go 1.7+ this is a No-OP, as the context is stored with the Request own context. In <1.7 this removes the context from the internal map

func (*State) Middleware Uses

func (s *State) Middleware(next http.Handler) http.Handler

Middleware wraps a handler with context creation and destruction.

Note that the user shouldn't use this one when using Ion, as the router inserts it by default (the router puts the path arguments in the context).

func (*State) WithContext Uses

func (s *State) WithContext(ctx interface{}, r *http.Request) *http.Request

WithContext associates the context with the request. It must be done once.

Package reqctx imports 2 packages (graph) and is imported by 1 packages. Updated 2017-01-06. Refresh now. Tools for package owners.