如何在另一个函数中使用已连接到我的 mongodb 和集合的句柄?

How do I use the handles that I have connected to my mongodb and collections in another function?

我有一个在 mongo 上设置的数据库,其中包含一些我需要通过端点的 url 参数查询的数据。为了使用该库,我定义了一些句柄,并在单独的 setup() 函数中完成了数据库连接的整个设置,但我无法在它之外使用我需要的句柄。

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "time"

    "github.com/gorilla/mux"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "go.mongodb.org/mongo-driver/mongo/readpref"
)

func setup() {
    clientOptions := options.Client().
        ApplyURI("mongodb+srv://<username>:<password>@cluster0.um5qb.mongodb.net/<db>?retryWrites=true&w=majority")
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    client, err := mongo.Connect(ctx, clientOptions)
    if err != nil {
        log.Fatal(err)
    }
    err = client.Ping(ctx, readpref.Primary())
    if err != nil {
        log.Fatal(err)
    }
    defer client.Disconnect(ctx)
    // DB := client.Database("cities-nighthack")
    // Cities := DB.Collection("city")

}

// model for user endpoint
type User struct {
    Email string `json:"email"`
}

// fake db to temp store users
var users []User

// checks if json is empty or not
func (u *User) IsEmpty() bool {
    return u.Email == ""
}

type App struct {
    Mongo *mongo.Client
}

func main() {
    setup()
    r := mux.NewRouter()
    r.HandleFunc("/user", createUser).Methods("POST")
    // r.HandleFunc("/suggest?city_name={city}", searchCity).Methods("GET")

    fmt.Println("Server running at port 8080")
    log.Fatal(http.ListenAndServe(":8080", r))

}

func createUser(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    if r.Body == nil {
        json.NewEncoder(w).Encode("Must send data")
    }

    var user User
    err := json.NewDecoder(r.Body).Decode(&user)
    if err != nil {
        log.Fatal(err)
    }
    if user.IsEmpty() {
        json.NewEncoder(w).Encode("Invalid! Enter user email.")
        return
    }
    users = append(users, user)
    json.NewEncoder(w).Encode(user)

}

func (a *App) searchCity(w http.ResponseWriter, r *http.Request) {

    vars := mux.Vars(r)
    city := vars["city"]
    
}

我想我可以像这样简单地传递处理程序:

func searchCity(city *mongo.Collection) (w http.ResponseWriter, r *http.Request) {
...
}

但是,gmux 不允许您这样做,因为它隐含地传递了 http.ResponseWriter*http.Request。因此,任何输入都不能在参数中。我尝试在全球范围内声明它们,但这没有用,建议不要这样做。有人告诉我可以尝试使用闭包或结构来传递它,但我也不太明白我将如何去做。

一种方法是这样的,首先添加一个服务器类型

type server struct {
    router *mux.Router
    cities *mongo.Collection
}

向服务器添加路由包装器

func (s *server) routes() {
    s.router.HandleFunc("/base", s.handleIndex()).Methods("GET")
}

处理函数

func (s *server) handleIndex() http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        cities := s.cities.Find(...) // something like that
        // write your response, etc
    }
}

然后在主

func main() {
    sr := &server{
        router: mux.NewRouter(),
        cities: getMongoDBCollection('cities') // implement this one :) should return a *mongo.Collection...
    }
    sr.routes()
...
}