8000 updated · wpcodevo/golang-mongodb-api@6436772 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6436772

Browse files
committed
updated
1 parent 56da824 commit 6436772

File tree

< 8000 span role="tooltip" aria-label="Collapse file tree" id="expand-button-file-tree-button" class="Tooltip__TooltipBase-sc-17tf59c-0 hWlpPn tooltipped-se">

4 files changed

+101
-87
lines changed

4 files changed

+101
-87
lines changed

controllers/auth.controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers
33
import (
44
"context"
55
"fmt"
6+
"html/template"
67
"log"
78
"net/http"
89
"strings"
@@ -23,10 +24,11 @@ type AuthController struct {
2324
userService services.UserService
2425
ctx context.Context
2526
collection *mongo.Collection
27+
temp *template.Template
2628
}
2729

28-
func NewAuthController(authService services.AuthService, userService services.UserService, ctx context.Context, collection *mongo.Collection) AuthController {
29-
return AuthController{authService, userService, ctx, collection}
30+
func NewAuthController(authService services.AuthService, userService services.UserService, ctx context.Context, collection *mongo.Collection, temp *template.Template) AuthController {
31+
return AuthController{authService, userService, ctx, collection, temp}
3032
}
3133

3234
func (ac *AuthController) SignUpUser(ctx *gin.Context) {
@@ -79,7 +81,7 @@ func (ac *AuthController) SignUpUser(ctx *gin.Context) {
7981
Subject: "Your account verification code",
8082
}
8183

82-
err = utils.SendEmail(newUser, &emailData, "verificationCode.html")
84+
err = utils.SendEmail(newUser, &emailData, ac.temp, "verificationCode.html")
8385
if err != nil {
8486
ctx.JSON(http.StatusBadGateway, gin.H{"status": "success", "message": "There was an error sending email"})
8587
return
@@ -269,7 +271,7 @@ func (ac *AuthController) ForgotPassword(ctx *gin.Context) {
269271
Subject: "Your password reset token (valid for 10min)",
270272
}
271273

272-
err = utils.SendEmail(user, &emailData, "resetPassword.html")
274+
err = utils.SendEmail(user, &emailData, ac.temp, "resetPassword.html")
273275
if err != nil {
274276
ctx.JSON(http.StatusBadGateway, gin.H{"status": "success", "message": "There was an error sending email"})
275277
return

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"html/template"
67
"log"
78
"net/http"
89

@@ -32,9 +33,12 @@ var (
3233
authService services.AuthService
3334
AuthController controllers.AuthController
3435
AuthRouteController routes.AuthRouteController
36+
37+
temp *template.Template
3538
)
3639

3740
func init() {
41+
temp = template.Must(template.ParseGlob("templates/*.html"))
3842
config, err := config.LoadConfig(".")
3943
if err != nil {
4044
log.Fatal("Could not load environment variables", err)
@@ -76,7 +80,7 @@ func init() {
7680
authCollection = mongoclient.Database("golang_mongodb").Collection("users")
7781
userService = services.NewUserServiceImpl(authCollection, ctx)
7882
authService = services.NewAuthService(authCollection, ctx)
79-
AuthController = controllers.NewAuthController(authService, userService, ctx, authCollection)
83+
AuthController = controllers.NewAuthController(authService, userService, ctx, authCollection, temp)
8084
AuthRouteController = routes.NewAuthRouteController(AuthController)
8185

8286
UserController = controllers.NewUserController(userService)

templates/resetPassword.html

Lines changed: 88 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,89 @@
1-
{{template "base" .}} {{define "content"}}
2-
<table role="presentation" class="main">
3-
<!-- START MAIN CONTENT AREA -->
4-
<tr>
5-
<td class="wrapper">
6-
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
7-
<tr>
8-
<td>
9-
<p>Hi {{ .FirstName}},</p>
10-
<p>
11-
Forgot password? Send a PATCH request to with your password and
12-
passwordConfirm to {{.URL}}
13-
</p>
14-
<table
15-
role="presentation"
16-
border="0"
17-
cellpadding="0"
18-
cellspacing="0"
19-
class="btn btn-primary"
20-
>
21-
<tbody>
22-
<tr>
23-
<td align="left">
24-
<table
25-
role="presentation"
26-
border="0"
27-
cellpadding="0"
28-
cellspacing="0"
29-
>
30-
<tbody>
31-
<tr>
32-
<td>
33-
<a href="{{.URL}}" target="_blank"
34-
>Reset password</a
35-
>
36-
</td>
37-
</tr>
38-
</tbody>
39-
</table>
40-
</td>
41-
</tr>
42-
</tbody>
43-
</table>
44-
<p>If you didn't forget your password, please ignore this email</p>
45-
<p>Good luck! Codevo CEO.</p>
46-
</td>
47-
</tr>
48-
</table>
49-
</td>
50-
</tr>
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6+
{{template "styles" .}}
7+
<title>{{ .Subject}}</title>
8+
</head>
9+
<body>
10+
<table
11+
role="presentation"
12+
border="0"
13+
cellpadding="0"
14+
cellspacing="0"
15+
class="body"
16+
>
17+
<tr>
18+
<td>&nbsp;</td>
19+
<td class="container">
20+
<div class="content">
21+
<!-- START CENTERED WHITE CONTAINER -->
22+
<table role="presentation" class="main">
23+
<!-- START MAIN CONTENT AREA -->
24+
<tr>
25+
<td class="wrapper">
26+
<table
27+
role="presentation"
28+
border="0"
29+
cellpadding="0"
30+
cellspacing="0"
31+
>
32+
<tr>
33+
<td>
34+
<p>Hi {{ .FirstName}},</p>
35+
<p>
36+
Forgot password? Send a PATCH request to with your
37+
password and passwordConfirm to {{.URL}}
38+
</p>
39+
<table
40+
role="presentation"
41+
border="0"
42+
cellpadding="0"
43+
cellspacing="0"
44+
class="btn btn-primary"
45+
>
46+
<tbody>
47+
<tr>
48+
<td align="left">
49+
<table
50+
role="presentation"
51+
border="0"
52+
cellpadding="0"
53+
cellspacing="0"
54+
>
55+
<tbody>
56+
<tr>
57+
<td>
58+
<a href="{{.URL}}" target="_blank"
59+
>Reset password</a
60+
>
61+
</td>
62+
</tr>
63+
</tbody>
64+
</table>
65+
</td>
66+
</tr>
67+
</tbody>
68+
</table>
69+
<p>
70+
If you didn't forget your password, please ignore this
71+
email
72+
</p>
73+
<p>Good luck! Codevo CEO.</p>
74+
</td>
75+
</tr>
76+
</table>
77+
</td>
78+
</tr>
5179

52-
<!-- END MAIN CONTENT AREA -->
53-
</table>
54-
{{end}}
80+
<!-- END MAIN CONTENT AREA -->
81+
</table>
82+
<!-- END CENTERED WHITE CONTAINER -->
83+
</div>
84+
</td>
85+
<td>&nbsp;</td>
86+
</tr>
87+
</table>
88+
</body>
89+
</html>

utils/email.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"crypto/tls"
66
"html/template"
77
"log"
8-
"os"
9-
"path/filepath"
108

119
"github.com/k3a/html2text"
1210
"github.com/wpcodevo/golang-mongodb/config"
@@ -21,27 +19,7 @@ type EmailData struct {
2119
}
2220

2321
// 👇 Email template parser
24-
25-
func ParseTemplateDir(dir string) (*template.Template, error) {
26-
var paths []string
27-
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
28-
if err != nil {
29-
return err
30-
}
31-
if !info.IsDir() {
32-
paths = append(paths, path)
33-
}
34-
return nil
35-
})
36-
37-
if err != nil {
38-
return nil, err
39-
}
40-
41-
return template.ParseFiles(paths...)
42-
}
43-
44-
func SendEmail(user *models.DBResponse, data *EmailData, templateName string) error {
22+
func SendEmail(user *models.DBResponse, data *EmailData, temp *template.Template, templateName string) error {
4523
config, err := config.LoadConfig(".")
4624

4725
if err != nil {
@@ -58,12 +36,7 @@ func SendEmail(user *models.DBResponse, data *EmailData, templateName string) er
5836

5937
var body bytes.Buffer
6038

61-
template, err := ParseTemplateDir("templates")
62-
if err != nil {
63-
log.Fatal("Could not parse template", err)
64-
}
65-
66-
if err := template.ExecuteTemplate(&body, templateName, &data); err != nil {
39+
if err := temp.ExecuteTemplate(&body, templateName, &data); err != nil {
6740
log.Fatal("Could not execute template", err)
6841
}
6942

0 commit comments

Comments
 (0)
0