Skip to content

Commit

Permalink
embed templates into bin
Browse files Browse the repository at this point in the history
Signed-off-by: William <[email protected]>
  • Loading branch information
kwesidev committed Aug 20, 2024
1 parent a3516ce commit ff7469c
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 4 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ FROM alpine:3.14
RUN apk add --no-cache bash
RUN apk add tzdata
COPY --from=build_base /tmp/speedy_auth/speedy_auth /app/speedy_auth
COPY --from=build_base /tmp/speedy_auth/static/email_templates/* /static/email_templates/
RUN cp /usr/share/zoneinfo/Africa/Johannesburg /etc/localtime
RUN echo "Africa/Johannesburg" > /etc/timezone
RUN cd /app
Expand Down
10 changes: 7 additions & 3 deletions internal/services/email_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"bytes"
"crypto/tls"
"embed"
"html/template"
"log"
"os"
Expand All @@ -12,6 +13,9 @@ import (
"gopkg.in/gomail.v2"
)

//go:embed email_templates
var staticFS embed.FS

type EmailService interface {
SendTwoFactorRequest(randomCode string, userDetails models.User) error
SendEmailLoginRequest(randomCode string, userDetails models.User) error
Expand Down Expand Up @@ -59,7 +63,7 @@ func (emSrv *emailService) sendEmail(to []string, subject, message string) error
func (emSrv *emailService) SendTwoFactorRequest(randomCode string, userDetails models.User) error {
var twoFactorRequestTemplateBuffer bytes.Buffer
// Get email template from directory and assign random code to it
emailTemplateFile, err := template.ParseFiles("static/email_templates/TwoFactorLogin.html")
emailTemplateFile, err := template.ParseFS(staticFS, "email_templates/TwoFactorLogin.html")
if err != nil {
return err
}
Expand All @@ -83,7 +87,7 @@ func (emSrv *emailService) SendTwoFactorRequest(randomCode string, userDetails m
func (emSrv *emailService) SendEmailLoginRequest(randomCode string, userDetails models.User) error {
var twoFactorRequestTemplateBuffer bytes.Buffer
// Get email template from directory and assign random code to it
emailTemplateFile, err := template.ParseFiles("static/email_templates/EmailLogin.html")
emailTemplateFile, err := template.ParseFS(staticFS, "email_templates/EmailLogin.html")
if err != nil {
return err
}
Expand All @@ -108,7 +112,7 @@ func (emSrv *emailService) SendEmailLoginRequest(randomCode string, userDetails
func (emSrv *emailService) SendPasswordResetRequest(randomCode string, userDetails models.User) error {
var passwordResetTemplateBuffer bytes.Buffer
// Get email template from directory and assign random code to it
emailTemplateFile, err := template.ParseFiles("static/email_templates/PasswordRequest.html")
emailTemplateFile, err := template.ParseFS(staticFS, "email_templates/PasswordRequest.html")
if err != nil {
log.Println("Template reading ", err)
return err
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit ff7469c

Please sign in to comment.