/context

Unobstructive context implementation for request-aware data sharing (merged into vinxi/vinxi)

Primary LanguageGoMIT LicenseMIT

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