Unobstructive context implementation for request-aware data sharing (merged into vinxi/vinxi)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
_examples/context
.editorconfig
.gitignore
.travis.yml
History.md
LICENSE
README.md
context.go
context_test.go
version.go

README.md

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