- The Go Programming Language (original) (raw)

Source file blog/context/gorilla/gorilla.go

 1  
 2  
 3  
 4  
 5  
 6  package gorilla
 7  
 8  import (
 9  	"net/http"
10  
11  	gcontext "github.com/gorilla/context"
12  	"golang.org/x/net/context"
13  )
14  
15  
16  
17  
18  func NewContext(parent context.Context, req *http.Request) context.Context {
19  	return &wrapper{parent, req}
20  }
21  
22  type wrapper struct {
23  	context.Context
24  	req *http.Request
25  }
26  
27  type key int
28  
29  const reqKey key = 0
30  
31  
32  
33  func (ctx *wrapper) Value(key interface{}) interface{} {
34  	if key == reqKey {
35  		return ctx.req
36  	}
37  	if val, ok := gcontext.GetOk(ctx.req, key); ok {
38  		return val
39  	}
40  	return ctx.Context.Value(key)
41  }
42  
43  
44  
45  func HTTPRequest(ctx context.Context) (*http.Request, bool) {
46  	
47  	
48  	
49  	req, ok := ctx.Value(reqKey).(*http.Request)
50  	return req, ok
51  }
52  

View as plain text