gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:07:26 25 4
gpt4 key购买 nike

我有一个在 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()
...
}

关于mongodb - 如何在另一个函数中使用连接到我的 mongodb 和集合的句柄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70296820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com