In my AuthMw middleware, I want to make database calls.
My database variable is initialized in main, how can I pass this to my middleware AuthMw?
func main() {
db, err := gorm.Open("postgres", ... )
r := mux.NewRouter()
r.Handle("/ws", serveWebsocket(hub))
r.Use(AuthMw)
//
//
...
} // main
func AuthMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
fmt.Printf("AuthMiddleware token is: %v\n", token)
ctx := ....
next.ServeHTTP(w, r.WithContext(ctx))
})
}