Re: [go-nuts] Dependency injection in gorillamux handlers

2019-09-25 Thread George Hartzell
Le lundi 23 septembre 2019 18:35:06 UTC+2, Abhinav Gupta a écrit : > [...] > myRouter.Handle("/endpoint1/", http.HandlerFunc(h.Endpoint1)).Methods("POST", > "OPTIONS") > // or > myRouter.HandleFunc("/endpoint1/", h.Endpoint1).Methods("POST", "OPTIONS") > Should that instead be (replacing 'h.Endpo

Re: [go-nuts] Dependency injection in gorillamux handlers

2019-09-25 Thread Nathanael Curin
Oh I love this! Not too verbose but still understandable, clean, and easy to add stuff. I didn't even think of using a method as a handler. Absolutely gonna go for this. Thanks! Le lundi 23 septembre 2019 18:35:06 UTC+2, Abhinav Gupta a écrit : > > You can place the dependencies into a struct an

Re: [go-nuts] Dependency injection in gorillamux handlers

2019-09-23 Thread 'Abhinav Gupta' via golang-nuts
You can place the dependencies into a struct and use a bound method on that as your handler. So you build a struct that will hold application-scoped objects like Redis client, content.Updater, etc. package contact type Handler struct { Redis *redis.Client Updater *content.Updater Ma

Re: [go-nuts] Dependency injection in gorillamux handlers

2019-09-23 Thread Nathanael Curin
I'm avoiding context.WithValue since I feel the objects I'm dealing with are too "powerful" (i.e. not simple Values). For a read, a 2016 article on this : https://peter.bourgon.org/blog/2016/07/11/context.html I remember reading through another article saying the usage of WithValue is kind of ri

Re: [go-nuts] Dependency injection in gorillamux handlers

2019-09-23 Thread Andrew Pillar
Why don't you use context.WithValue(r.Context(), , ) for passing the injected values you want to your HTTP handlers. Then defer the logic for retrieving whatever you want for that handler to a middleware function. -- You received this message because you are subscribed to the Google Groups "gola

[go-nuts] Dependency injection in gorillamux handlers

2019-09-23 Thread Nathanael Curin
Hi everyone, I'm currently using the gorilla/mux package to make an API with a few handlers that depend on multiple external resources (a Redis DB for example). The fact is, HTTP Handlers only use http.ResponseWriter and *http.Request as parameter, so I used some random dependency injection pa