如何在Go中惯用地将数据库访问转换为功能

时间:2019-02-05 07:37:03

标签: go mongo-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)
}

我了解我可以创建一个返回(* mongoClient,err)的方法,因为稍后我在代码中使用了客户端局部变量。

但是我对如何实现defer cancel()部分一无所知,因为一旦方法CreateStudenAccountEndpoint结束,它就会执行。但是我对如何在一种方法中实现此defer部分感到困惑,该方法将认识到我希望延迟发生在调用DB访问层方法的函数的末尾,例如CreateStudentAccountEndpoint实际的数据库访问方法本身。

1 个答案:

答案 0 :(得分:0)

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

通常看起来像这样:

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的调用,但我认为,只有在遇到故障后,您才能尝试通过重新连接来恢复连接。