On 2012/11/10 Kim Eik <
k...@heldig.org> wrote:
> No, not really, as i need the session in a http handler function. The
> handler function only accepts w ResponseWriter, req *Request. Now, if there
> was only some way of expanding this to include a context object that could
> hold a mgo session, then that would be great. But as far as i know, i don't
> think this is possible. At least not directly.
>
> However, i could probably create my own handler object that just proxies the
> original handler and supplies a context. But im not sure of how to proceed
> with this.
>
> Is there any other ways of getting the session from the handler function,
> instead of trying to inject it somehow?
You can use a net/http.Handler
(
http://golang.org/pkg/net/http/#Handler) or a closure.
For example, you other package could do:
func HandleHTTP(ses *mgo.Session) {
http.HandleFunc("/my/path", func(w http.ResponseWriter, req *http.Request) {
DoSomethingWithSessionAndRequest(ses, w, req)
})
}
Or if you need a handler factory:
type myHandler struct {
Session *mgo.Session
}
func (h myHandler) Handle(w http.ResponseWriter, req *http.Request) {
blah blah
}
func MakeHTTPHandler(ses *mgo.Session) {
return myHandler{Session: ses}
}
Rémy.