I'm writing a web app using chi router and have global vars for the database connection string and gorilla sessions mysqlstore. I want to pass these to the chi mount function instead of using global vars but can't figure out how to do it.
Here is my code, redacted for brevity.
var dBSource string
var store *mysqlstore.MySQLStore
func main() {
var err error
dBSource = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbUser, dbPass, dbHost, dbPort, dbName)
store, err = mysqlstore.NewMySQLStore(endpoint, tableName, path, maxAge, codecs)
defer store.Close()
r := chi.NewRouter()
r.Mount("/users", usersResource{}.Routes())
}
type usersResource struct{}
func (rs usersResource) Routes() chi.Router {
r := chi.NewRouter()
r.Get("/", rs.List)
return r
}
func (rs usersResource) List(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "session-data")
db, err := sql.Open("mysql", dBSource)
}