从AppEngine的上下文中获取* http.Request

I'm using app engine, and creating context.Context (golang.org/x/net/context) variable from the *http.Request.

    c := appengine.NewContext(r)

I'm passing the context around and I'm trying to figure out a way to get the *http.Request from the context.Context in order to log the http.Request.

I search all over the doc but I couldn't find any solution.

appengine.NewContext(r) returns a value of type appengine.Context. This is not the same as the Context type of the golang.org/x/net/context package!

Having a value of type appengine.Context, you can't get the *http.Request you used to create it. If you will need the *http.Request, you have to take care of passing that around yourself (you have it, since you use that to create the context).

Note that appengine.Context (which is an interface type) has a method Context.Request(), but that is for internal use only, it is not exported for anyone to call it. And also it returns an interface{} and not a *http.Request. Even if it returns a value holding a *http.Request, you can't rely on it as this method may be changed or removed in future versions.

Passing the *http.Request along with the appengine.Context is the best way. Trying to get it from the context is just "wizardry" and might break with a new SDK release. If you want to simplify it, you may create a wrapper struct and pass that wrapper instead of 2 values, for example:

type Wrapper struct {
    C appengine.Context
    R *http.Request
}

And a helper func:

func CreateCtx(r *http.Request) Wrapper {
    return Wrapper{appengine.NewContext(r), r}
}