如何在 Go 中以惯用的方式将数据库访问权限转换为函数

How to turn DataBase access into a Function idiomatically in Go

我在 Go 中构建了一个后端 API,它可以工作,但是我想将数据库访问层的代码重构为一个函数 - 惯用地。

// Get the form data entered by client; FirstName, LastName, phone Number,
// assign the person a unique i.d
// check to see if that user isn't in the database already
// if they are send an error message with the a  'bad' response code
// if they aren't in db add to db and send a message with success
func CreateStudentAccountEndpoint(response http.ResponseWriter, request *http.Request){

    client, err := mongo.NewClient("mongodb://localhost:27017")
    if err != nil {
        log.Fatalf("Error connecting to mongoDB client Host: Err-> %v\n ", err)
    }
    ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
    defer cancel()
    err = client.Connect(ctx)
    if err != nil {
        log.Fatalf("Error Connecting to MongoDB at context.WtihTimeout: Err-> %v\n ", err)
    }


    response.Header().Set("Content-Type", "application/json")

    studentCollection := client.Database(dbName).Collection("students")
    _, err = studentCollection.InsertOne(context.Background(),data)
    if err != nil {
        response.WriteHeader(501)
        response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
    }
    // encoding json object for returning to the client
    jsonStudent, err := json.Marshal(student)
    if err != nil {
        http.Error(response, err.Error(), http.StatusInternalServerError)
    }

    response.Write(jsonStudent)
}

我知道我可以创建一个方法 returns (*mongoClient, err) 因为我稍后会在代码中使用客户端局部变量。

但是我不知道如何实现 defer cancel() 部分,因为它会在方法 CreateStudenAccountEndpoint 结束时执行。但是我不知道如何在一个方法中实现这个 defer 部分,该方法将识别我希望延迟发生在调用数据库访问层方法的函数的末尾,例如 CreateStudentAccountEndpoint 不是实际的数据库访问方法本身。

据我了解,连接应该是长期存在的,并设置为构造函数的一部分,即不是请求流的一部分。

这通常看起来像这样:

type BackendAPI struct {
    client *mongo.Client
}

func NewBackendAPI(mongoURI string) (*BackendAPI, error) {
    client, err := mongo.NewClient(mongoURI)
    if err != nil {
        return nil, err
    }
    ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
    defer cancel()
    err = client.Connect(ctx)
    if err != nil {
        return nil, err
    }

    return &BackendAPI{client}, nil
}

func (api *BackendAPI) func CreateStudentAccountEndpoint(response http.ResponseWriter, request *http.Request) {
    response.Header().Set("Content-Type", "application/json")

    // note the use of the long-lived api.client, which is connected already.
    studentCollection := api.client.Database(dbName).Collection("students")
    _, err = studentCollection.InsertOne(context.Background() ,data)
    if err != nil {
        response.WriteHeader(501)
        response.Write([]byte(`{ "message": "` + err.Error() + `" }`))
        return // at this point, the method should return
    }
    // encoding json object for returning to the client
    jsonStudent, err := json.Marshal(student)
    if err != nil {
        http.Error(response, err.Error(), http.StatusInternalServerError)
    }

    response.Write(jsonStudent)
}

如果您担心失去连接,您可以在其中实现对 api.client.Ping 的调用,但在我看来,只有在遇到您认为可以通过重新连接恢复的故障时才应尝试这样做。