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
Mapper *mapping.Updater
}
func (h *Handler) Endpoint1(w http.ResponseWriter, r *http.Request) {
// ...
}
Then build the Handler
when you’re setting up the router (presumably in
main()
or nearby) and register it against the router as usual.
handler := contact.Handler{
Redis: config.GetRedisDb(),
Updater: contents.GetUpdater(),
Mapper: mapping.GetUpdater(),
}
myRouter.Handle("/endpoint1/", http.HandlerFunc(h.Endpoint1)).Methods("POST", "OPTIONS")
// or
myRouter.HandleFunc("/endpoint1/", h.Endpoint1).Methods("POST", "OPTIONS")
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c23761e8-1287-4758-8303-b39db8cf22f0%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.