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

This commit is contained in:
Maxime Delporte
2025-11-30 17:22:32 +01:00
parent 68e606976f
commit 8954318e40
4 changed files with 47 additions and 3 deletions

View File

@@ -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.