Как превратить доступ к базе данных в функцию идиоматически в Go - PullRequest
0 голосов
/ 05 февраля 2019

Я создал 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, а не фактический доступ к БДсам метод.

1 Ответ

0 голосов
/ 05 февраля 2019

Насколько я понимаю, соединение должно быть долгоживущим и настроено как часть конструктора, т.е. не как часть потока запросов.

Обычно это будет выглядеть примерно так:

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 там, но, по моему мнению, это следует делать только в том случае, если вы столкнетесь с ошибкой, от которой, по вашему мнению, вы сможете восстановить, восстановив соединение.

...