chore: extract mail to it's own service
Some checks failed
Build Docker Image / Explore-Gitea-Actions (push) Failing after 45s
Some checks failed
Build Docker Image / Explore-Gitea-Actions (push) Failing after 45s
This commit is contained in:
@@ -73,7 +73,7 @@ func (handler HandlerAuthImpl) handleSignIn() http.HandlerFunc {
|
|||||||
http.Error(w, "Invalid email or password", http.StatusUnauthorized)
|
http.Error(w, "Invalid email or password", http.StatusUnauthorized)
|
||||||
} else {
|
} else {
|
||||||
utils.LogError("Error signing in", err)
|
utils.LogError("Error signing in", err)
|
||||||
http.Error(w, "An error occurred", http.StatusInternalServerError)
|
http.Error(w, "InternalServerErr", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
11
handler/auth_test.go
Normal file
11
handler/auth_test.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandleSignIn(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
t.Run("should signIn and return session cookie", func(t *testing.T) {
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ func GetHandler(d *sql.DB) http.Handler {
|
|||||||
|
|
||||||
router.HandleFunc("/", service.HandleIndexAnd404(d))
|
router.HandleFunc("/", service.HandleIndexAnd404(d))
|
||||||
|
|
||||||
handlerAuth := NewHandlerAuth(d, service.NewServiceAuthImpl(db.NewDbAuthSqlite(d)))
|
handlerAuth := NewHandlerAuth(d, service.NewServiceAuthImpl(db.NewDbAuthSqlite(d), service.NewServiceMail()))
|
||||||
|
|
||||||
// Serve static files (CSS, JS and images)
|
// Serve static files (CSS, JS and images)
|
||||||
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
||||||
|
|||||||
@@ -49,11 +49,13 @@ type ServiceAuth interface {
|
|||||||
|
|
||||||
type ServiceAuthImpl struct {
|
type ServiceAuthImpl struct {
|
||||||
dbAuth db.DbAuth
|
dbAuth db.DbAuth
|
||||||
|
mail ServiceMail
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServiceAuthImpl(dbAuth db.DbAuth) *ServiceAuthImpl {
|
func NewServiceAuthImpl(dbAuth db.DbAuth, mail ServiceMail) *ServiceAuthImpl {
|
||||||
return &ServiceAuthImpl{
|
return &ServiceAuthImpl{
|
||||||
dbAuth: dbAuth,
|
dbAuth: dbAuth,
|
||||||
|
mail: mail,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,7 +401,8 @@ func HandleDeleteAccountComp(db *sql.DB) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go utils.SendMail(user.Email, "Account deleted", "Your account has been deleted")
|
//TODO
|
||||||
|
// go utils.SendMail(user.Email, "Account deleted", "Your account has been deleted")
|
||||||
|
|
||||||
utils.DoRedirect(w, r, "/")
|
utils.DoRedirect(w, r, "/")
|
||||||
}
|
}
|
||||||
@@ -577,7 +580,8 @@ func HandleResetPasswordComp(db *sql.DB) http.HandlerFunc {
|
|||||||
utils.TriggerToast(w, r, "error", "Internal Server Error")
|
utils.TriggerToast(w, r, "error", "Internal Server Error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
utils.SendMail(email, "Reset Password", mail.String())
|
//TODO
|
||||||
|
// utils.SendMail(email, "Reset Password", mail.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.TriggerToast(w, r, "info", "If the email exists, an email has been sent")
|
utils.TriggerToast(w, r, "info", "If the email exists, an email has been sent")
|
||||||
@@ -612,7 +616,8 @@ func sendVerificationEmail(db *sql.DB, userId string, email string) {
|
|||||||
utils.LogError("Could not render welcome email", err)
|
utils.LogError("Could not render welcome email", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
utils.SendMail(email, "Welcome to ME-FIT", w.String())
|
//TODO
|
||||||
|
// utils.SendMail(email, "Welcome to ME-FIT", w.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TryCreateSessionAndSetCookie(r *http.Request, w http.ResponseWriter, db *sql.DB, user_uuid uuid.UUID) error {
|
func TryCreateSessionAndSetCookie(r *http.Request, w http.ResponseWriter, db *sql.DB, user_uuid uuid.UUID) error {
|
||||||
|
|||||||
93
service/mail.go
Normal file
93
service/mail.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"me-fit/utils"
|
||||||
|
"net/smtp"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ServiceMail interface {
|
||||||
|
SendMail(to string, subject string, message string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServiceMail() ServiceMail {
|
||||||
|
enabled := os.Getenv("SMTP_ENABLED") == "true"
|
||||||
|
host := os.Getenv("SMTP_HOST")
|
||||||
|
port := os.Getenv("SMTP_PORT")
|
||||||
|
user := os.Getenv("SMTP_USER")
|
||||||
|
pass := os.Getenv("SMTP_PASS")
|
||||||
|
fromMail := os.Getenv("SMTP_FROM_MAIL")
|
||||||
|
fromName := os.Getenv("SMTP_FROM_NAME")
|
||||||
|
|
||||||
|
if enabled {
|
||||||
|
if host == "" {
|
||||||
|
log.Fatal("SMTP_HOST must be set")
|
||||||
|
}
|
||||||
|
if port == "" {
|
||||||
|
log.Fatal("SMTP_PORT must be set")
|
||||||
|
}
|
||||||
|
if user == "" {
|
||||||
|
log.Fatal("SMTP_USER must be set")
|
||||||
|
}
|
||||||
|
if pass == "" {
|
||||||
|
log.Fatal("SMTP_PASS must be set")
|
||||||
|
}
|
||||||
|
if fromMail == "" {
|
||||||
|
log.Fatal("SMTP_FROM_MAIL must be set")
|
||||||
|
}
|
||||||
|
if fromName == "" {
|
||||||
|
log.Fatal("SMTP_FROM_NAME must be set")
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewServiceMailSmtp(host, port, user, pass, fromMail, fromName)
|
||||||
|
} else {
|
||||||
|
if utils.Environment == "prod" {
|
||||||
|
log.Fatal("SMTP_ENABLED must be set to true in production")
|
||||||
|
}
|
||||||
|
return NewServiceMailStub()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServiceMailSmtp struct {
|
||||||
|
host string
|
||||||
|
port string
|
||||||
|
auth smtp.Auth
|
||||||
|
fromMail string
|
||||||
|
fromName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServiceMailSmtp(
|
||||||
|
host string,
|
||||||
|
port string,
|
||||||
|
user string,
|
||||||
|
pass string,
|
||||||
|
fromMail string,
|
||||||
|
fromName string) ServiceMailSmtp {
|
||||||
|
return ServiceMailSmtp{
|
||||||
|
host: host,
|
||||||
|
port: port,
|
||||||
|
auth: smtp.PlainAuth("", user, pass, host),
|
||||||
|
fromMail: fromMail,
|
||||||
|
fromName: fromName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service ServiceMailSmtp) SendMail(to string, subject string, message string) error {
|
||||||
|
|
||||||
|
msg := fmt.Sprintf("From: %v <%v>\nTo: %v\nSubject: %v\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n%v", service.fromName, service.fromMail, to, subject, message)
|
||||||
|
|
||||||
|
return smtp.SendMail(service.host+":"+service.port, service.auth, service.fromMail, []string{to}, []byte(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServiceMailStub struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServiceMailStub() ServiceMailStub {
|
||||||
|
return ServiceMailStub{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service ServiceMailStub) SendMail(to string, subject string, message string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
34
utils/env.go
34
utils/env.go
@@ -7,44 +7,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
SmtpHost string
|
BaseUrl string
|
||||||
SmtpPort string
|
Environment string
|
||||||
SmtpUser string
|
|
||||||
SmtpPass string
|
|
||||||
SmtpFromMail string
|
|
||||||
SmtpFromName string
|
|
||||||
BaseUrl string
|
|
||||||
Environment string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func MustInitEnv() {
|
func MustInitEnv() {
|
||||||
SmtpHost = os.Getenv("SMTP_HOST")
|
|
||||||
SmtpPort = os.Getenv("SMTP_PORT")
|
|
||||||
SmtpUser = os.Getenv("SMTP_USER")
|
|
||||||
SmtpPass = os.Getenv("SMTP_PASS")
|
|
||||||
SmtpFromMail = os.Getenv("SMTP_FROM_MAIL")
|
|
||||||
SmtpFromName = os.Getenv("SMTP_FROM_NAME")
|
|
||||||
BaseUrl = os.Getenv("BASE_URL")
|
BaseUrl = os.Getenv("BASE_URL")
|
||||||
Environment = os.Getenv("ENVIRONMENT")
|
Environment = os.Getenv("ENVIRONMENT")
|
||||||
|
|
||||||
if SmtpHost == "" {
|
|
||||||
log.Fatal("SMTP_HOST must be set")
|
|
||||||
}
|
|
||||||
if SmtpPort == "" {
|
|
||||||
log.Fatal("SMTP_PORT must be set")
|
|
||||||
}
|
|
||||||
if SmtpUser == "" {
|
|
||||||
log.Fatal("SMTP_USER must be set")
|
|
||||||
}
|
|
||||||
if SmtpPass == "" {
|
|
||||||
log.Fatal("SMTP_PASS must be set")
|
|
||||||
}
|
|
||||||
if SmtpFromMail == "" {
|
|
||||||
log.Fatal("SMTP_FROM_MAIL must be set")
|
|
||||||
}
|
|
||||||
if SmtpFromName == "" {
|
|
||||||
log.Fatal("SMTP_FROM_NAME must be set")
|
|
||||||
}
|
|
||||||
if BaseUrl == "" {
|
if BaseUrl == "" {
|
||||||
log.Fatal("BASE_URL must be set")
|
log.Fatal("BASE_URL must be set")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/smtp"
|
|
||||||
)
|
|
||||||
|
|
||||||
func SendMail(to string, subject string, message string) error {
|
|
||||||
|
|
||||||
auth := smtp.PlainAuth("", SmtpUser, SmtpPass, SmtpHost)
|
|
||||||
|
|
||||||
msg := fmt.Sprintf("From: %v <%v>\nTo: %v\nSubject: %v\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n%v", SmtpFromName, SmtpFromMail, to, subject, message)
|
|
||||||
|
|
||||||
return smtp.SendMail(SmtpHost+":"+SmtpPort, auth, SmtpFromMail, []string{to}, []byte(msg))
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user