context

package module
v0.0.0-...-1c7391a Latest Latest
Warning

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

Go to latest
Published: May 8, 2016 License: MIT Imports: 2 Imported by: 0

README

context Build Status GoDoc API Coverage Status Go Report Card

Note: merged into vinxi/vinxi.

context package implements a simple, unobstructive context for request-aware data sharing across a middleware pipeline.

Originally based in nbio/httpcontext.

Installation

go get -u gopkg.in/vinxi/context.v0

API

See godoc reference.

Example

package main

import (
  "fmt"
  "gopkg.in/vinxi/context.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  fmt.Printf("Server listening on port: %d\n", 3100)
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  vs.Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
    context.Set(r, "foo", "bar")
    h.ServeHTTP(w, r)
  })

  vs.Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
    w.Header().Set("foo", context.GetString(r, "foo"))
    h.ServeHTTP(w, r)
  })

  vs.Forward("http://httpbin.org")

  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

License

MIT

Documentation

Overview

Package context implements a simple request-aware HTTP context designed to be used via middleware layer to share polymorfic data.

context is not thread-safe by default.

Index

Constants

View Source
const Version = "0.1.0"

Version stores the current package semantic version.

Variables

This section is empty.

Functions

func Clear

func Clear(req *http.Request)

Clear clears all stored values from a request’s context.

func Delete

func Delete(req *http.Request, key interface{})

Delete deletes a stored value from a request’s context.

func Get

func Get(req *http.Request, key interface{}) interface{}

Get gets a context value from req. Returns nil if key not found in the request context.

func GetAll

func GetAll(req *http.Request) map[interface{}]interface{}

GetAll returns all stored context values for a request. Will always return a valid map. Returns an empty map for requests context data previously set.

func GetError

func GetError(req *http.Request, key interface{}) error

GetError gets a error context value from req. Returns nil if key not found in the request context, or the value does not evaluate to a error.

func GetOk

func GetOk(req *http.Request, key interface{}) (val interface{}, ok bool)

GetOk gets a context value from req. Returns (nil, false) if key not found in the request context.

func GetString

func GetString(req *http.Request, key interface{}) string

GetString gets a string context value from req. Returns an empty string if key not found in the request context, or the value does not evaluate to a string.

func Set

func Set(req *http.Request, key interface{}, value interface{})

Set sets a context value on req. It currently accomplishes this by replacing the http.Request’s Body with a ReadCloser, which wraps the original io.ReadCloser. See “Invasion of the Body Snatchers.”

Types

type ReadCloser

type ReadCloser interface {
	io.ReadCloser
	Context() map[interface{}]interface{}
}

ReadCloser augments the io.ReadCloser interface with a Context() method.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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