Adding godotenv library to use our .env file for the smtp configuration. Updating main.go adding our logger first, load our .env file, retrieve values to initialize the mail application's struct. Updating registerUserHandler sending the user_welcome.tmpl email.
Some checks failed
Deploy Greenlight API / deploy (push) Failing after 15m11s
Some checks failed
Deploy Greenlight API / deploy (push) Failing after 15m11s
This commit is contained in:
@@ -6,9 +6,12 @@ import (
|
||||
"flag"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"greenlight.craftr.fr/internal/data"
|
||||
"greenlight.craftr.fr/internal/mailer"
|
||||
|
||||
// Import the pq driver so that it can register itself with the database/sql package. Note that we alias this import to the blank identifier, to stop the Go compiler complaining that the package isn't being used.
|
||||
_ "github.com/lib/pq"
|
||||
@@ -44,6 +47,13 @@ type config struct {
|
||||
burst int
|
||||
enabled bool
|
||||
}
|
||||
smtp struct {
|
||||
host string
|
||||
port int
|
||||
username string
|
||||
password string
|
||||
sender string
|
||||
}
|
||||
}
|
||||
|
||||
// application struct hold the dependencies for our HTTP handlers, helpers, and middleware.
|
||||
@@ -51,9 +61,18 @@ type application struct {
|
||||
config config
|
||||
logger *slog.Logger
|
||||
models data.Models
|
||||
mailer mailer.Mailer
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Initialize a new structured logger which writes log entries to standard out stream.
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
logger.Error("Error loading .env file")
|
||||
}
|
||||
|
||||
// Declare an instance of the config struct.
|
||||
var cfg config
|
||||
|
||||
@@ -78,10 +97,24 @@ func main() {
|
||||
flag.IntVar(&cfg.limiter.burst, "limiter-burst", 4, "Rate limiter maximum burst")
|
||||
flag.BoolVar(&cfg.limiter.enabled, "limiter-enabled", true, "Enable rate limiter")
|
||||
|
||||
flag.Parse()
|
||||
smtpHost := os.Getenv("SMTP_HOST")
|
||||
smtpPort := os.Getenv("SMTP_PORT")
|
||||
smtpPortInt, err := strconv.Atoi(smtpPort)
|
||||
if err != nil {
|
||||
logger.Error("couldn't convert smtpPort to integer.")
|
||||
}
|
||||
smtpUsername := os.Getenv("SMTP_USERNAME")
|
||||
smtpPassword := os.Getenv("SMTP_PASSWORD")
|
||||
smtpSender := os.Getenv("SMTP_SENDER")
|
||||
|
||||
// Initialize a new structured logger which writes log entries to standard out stream.
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
// Read the SMTP server configuration settings into the config struct, using the Mailtrap settings as the default values.
|
||||
flag.StringVar(&cfg.smtp.host, "smtp-host", smtpHost, "SMTP host")
|
||||
flag.IntVar(&cfg.smtp.port, "smtp-port", smtpPortInt, "SMTP port")
|
||||
flag.StringVar(&cfg.smtp.username, "smtp-username", smtpUsername, "SMTP username")
|
||||
flag.StringVar(&cfg.smtp.password, "smtp-password", smtpPassword, "SMTP password")
|
||||
flag.StringVar(&cfg.smtp.sender, "smtp-sender", smtpSender, "SMTP sender")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// Call the openDB() helper function to create the connection pool, passing in the config struct. If this returns an error, we log it and exit the application immediately.
|
||||
db, err := openDB(cfg)
|
||||
@@ -101,6 +134,7 @@ func main() {
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
models: data.NewModels(db),
|
||||
mailer: mailer.New(cfg.smtp.host, cfg.smtp.port, cfg.smtp.username, cfg.smtp.password, cfg.smtp.sender),
|
||||
}
|
||||
|
||||
// Call app.serve() to start the server.
|
||||
|
||||
@@ -59,6 +59,13 @@ func (app *application) registerUserHandler(w http.ResponseWriter, r *http.Reque
|
||||
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.
|
||||
err = app.mailer.Send(user.Email, "user_welcome.tmpl", user)
|
||||
if err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Write a JSON response containing the user data along with a 201 Created status code
|
||||
err = app.writeJSON(w, http.StatusCreated, envelope{"user": user}, nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user