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 
pattern I've found online.

The structure of my project goes as follows :
- subpackage "contact" containing the HTTP Handler as well as the Inject 
method shown below
- subpackage "config" containing a goroutine using fsnotify+mutex to 
refresh internal configuration if modified + an opened connection to a 
*redis.Client
- subpackage "mapping" which handles other files on the system in the same 
way as Config ; updates an internal map[] with new data
- subpackage "contents", same idea, mysql data source to refresh a map 
every once in a while

Here's a few code showing the current flow :

*- Router initialization :*

myRouter.Handle("/endpoint1/", contact.Inject(config.GetRedisDb(), 
contents.GetUpdater(), mapping.GetUpdater(), 
contact.Handle)).Methods("POST", "OPTIONS")

*- contact.Inject code :*

func Inject(
    red *redis.Client,
    u *content.Updater,
    m *mapping.Updater,
    f func(red *redis.Client, u *content.Updater, mapper *mapping.Updater, 
w http.ResponseWriter, r *http.Request),
) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        f(red, u, m, w, r)
    })
}

*- contact.Handle signature :*

func Handle(rdsClient *redis.Client, u *content.Updater, m 
*mapping.Updater, w http.ResponseWriter, r *http.Request) {}

My issue is that the code feels quite "heavy". It works quite well and I'm 
pretty sure performance impact is quite minimal by injecting resources this 
way, but readability is quite bad, and now that I actually need to add a 
completely new dependency, I'm not sure I want to keep doing this.

Is there any better way you guys know of? Is my method being annoying to 
read through still considered as good?

Thanks!

Nathanael




-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e181bdbf-8fc9-49c9-9053-7e9c24d48602%40googlegroups.com.

Reply via email to