Sending background emails.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 2m54s
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 2m54s
This commit is contained in:
@@ -4,13 +4,14 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/julienschmidt/httprouter"
|
|
||||||
"greenlight.craftr.fr/internal/validator"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"greenlight.craftr.fr/internal/validator"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -204,3 +205,19 @@ func (app *application) readInt(qs url.Values, key string, defaultValue int, v *
|
|||||||
// Otherwise, return the converted integer value
|
// Otherwise, return the converted integer value
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Background: accepts an arbitrary function as a parameter
|
||||||
|
func (app *application) background(fn func()) {
|
||||||
|
// Launch a background goroutine
|
||||||
|
go func() {
|
||||||
|
// recover any panic.
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
app.logger.Error("%v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Execute the arbitrary function that we passed as the parameter
|
||||||
|
fn()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|||||||
@@ -59,15 +59,19 @@ func (app *application) registerUserHandler(w http.ResponseWriter, r *http.Reque
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the Send() method on our Mail, passing in the user's email address, name of the template file, and the User struct containing the new user's data.
|
// Use the background helper to execute an anonymous function that send the welcome email
|
||||||
err = app.mailer.Send(user.Email, "user_welcome.tmpl", user)
|
app.background(func() {
|
||||||
if err != nil {
|
// Call the Send() method on our Mail, passing in the user's email address, name of the template file, and the User struct containing the new user's data.
|
||||||
app.serverErrorResponse(w, r, err)
|
err = app.mailer.Send(user.Email, "user_welcome.tmpl", user)
|
||||||
return
|
|
||||||
}
|
if err != nil {
|
||||||
|
// Importantly, if there is an error sending the email, then we use the app.logger.Error() helper to manage it, instead of the app.serverErrorResponse() helper like before.
|
||||||
|
app.logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Write a JSON response containing the user data along with a 201 Created status code
|
// Not that we also change this to send the client a 202 Accepted status code. This status code indicates that the request has been accepted for processing, but the processing has not been completed.
|
||||||
err = app.writeJSON(w, http.StatusCreated, envelope{"user": user}, nil)
|
err = app.writeJSON(w, http.StatusAccepted, envelope{"user": user}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.serverErrorResponse(w, r, err)
|
app.serverErrorResponse(w, r, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user