Я создал Backend API в Go, он работает, однако я хочу, чтобы рефакторинг кода для уровня доступа к БД в функцию - идиоматически.
// 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
в методе, который распознает, что я хочу, чтобы отсрочка происходила в конце функции, которая вызывает метод уровня доступа к БД, например CreateStudentAccountEndpoint
, а не фактический доступ к БДсам метод.