added config for helper service
This commit is contained in:
parent
6509ddd0ae
commit
34cc3c9b50
9
helper/config.yaml
Normal file
9
helper/config.yaml
Normal file
@ -0,0 +1,9 @@
|
||||
kafka:
|
||||
brokers:
|
||||
- localhost:9092
|
||||
topic: backend_events
|
||||
smtp:
|
||||
server: smtp.yandex.ru
|
||||
port: 587
|
||||
email: ""
|
||||
password: ""
|
||||
@ -9,4 +9,5 @@ require (
|
||||
github.com/pierrec/lz4/v4 v4.1.15 // indirect
|
||||
github.com/segmentio/kafka-go v0.4.47 // indirect
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@ -60,4 +60,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@ -5,58 +5,73 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/segmentio/kafka-go"
|
||||
"gopkg.in/gomail.v2"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// type emailHelper struct {
|
||||
// dialer *gomail.Dialer
|
||||
// }
|
||||
const MSG_TEXT = `
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<p>This message was sent because you forgot a password</p>
|
||||
<p>To change a password, use <a href="{{Link}}"/>this</a> link</p>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
// func (e *emailHelper) SendEmailForgotPassword(email, token string) {
|
||||
// link := "https://nucrea.ru?token=" + token
|
||||
func SendEmailForgotPassword(dialer *gomail.Dialer, from, to, token string) error {
|
||||
link := "localhost:8080/restore-password?token=" + token
|
||||
|
||||
// const MSG_TEXT = `
|
||||
// <html>
|
||||
// <head>
|
||||
// </head>
|
||||
// <body>
|
||||
// <p>This message was sent because you forgot a password</p>
|
||||
// <p>To change a password, use <a href="{{Link}}"/>this</a> link</p>
|
||||
// </body>
|
||||
// </html>
|
||||
// `
|
||||
// msgText := strings.ReplaceAll(MSG_TEXT, "{{Link}}", link)
|
||||
msgText := strings.ReplaceAll(MSG_TEXT, "{{Link}}", link)
|
||||
|
||||
// m := gomail.NewMessage()
|
||||
// m.SetHeader("From", "email")
|
||||
// m.SetHeader("To", email)
|
||||
// m.SetHeader("Subject", "Hello!")
|
||||
// m.SetBody("text/html", msgText)
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("From", m.FormatAddress(from, "Pet Backend"))
|
||||
m.SetHeader("To", to)
|
||||
m.SetHeader("Subject", "Hello!")
|
||||
m.SetBody("text/html", msgText)
|
||||
|
||||
// if err := d.DialAndSend(m); err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// }
|
||||
return dialer.DialAndSend(m)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Kafka struct {
|
||||
Brokers []string `yaml:"brokers"`
|
||||
Topic string `yaml:"topic"`
|
||||
}
|
||||
|
||||
SMTP struct {
|
||||
Server string `yaml:"server"`
|
||||
Port int `yaml:"port"`
|
||||
Email string `yaml:"email"`
|
||||
Password string `yaml:"password"`
|
||||
} `yaml:"smtp"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
const (
|
||||
SMTP_SERVER = "smtp.yandex.ru"
|
||||
SMTP_PORT = 587
|
||||
SMTP_LOGIN = "serghio2@yandex.ru"
|
||||
SMTP_PASSWORD = "ercutguhcfuzbvyl"
|
||||
)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
d := gomail.NewDialer(SMTP_SERVER, SMTP_PORT, SMTP_LOGIN, SMTP_PASSWORD)
|
||||
configFile, err := os.ReadFile("config.yaml")
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
config := &Config{}
|
||||
if err := yaml.Unmarshal(configFile, config); err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
dialer := gomail.NewDialer(config.SMTP.Server, config.SMTP.Port, config.SMTP.Email, config.SMTP.Password)
|
||||
|
||||
log.Println("starting reader...")
|
||||
|
||||
r := kafka.NewReader(kafka.ReaderConfig{
|
||||
Brokers: []string{"localhost:9092"},
|
||||
Topic: "backend_events",
|
||||
Brokers: config.Kafka.Brokers,
|
||||
Topic: config.Kafka.Topic,
|
||||
GroupID: "consumer-group-id",
|
||||
})
|
||||
|
||||
@ -90,13 +105,7 @@ func main() {
|
||||
continue
|
||||
}
|
||||
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("From", m.FormatAddress("serghio2@yandex.ru", "Pet Backend"))
|
||||
m.SetHeader("To", value.Email)
|
||||
m.SetHeader("Subject", "Hello!")
|
||||
m.SetBody("text/html", "Test backend")
|
||||
|
||||
if err := d.DialAndSend(m); err != nil {
|
||||
if err := SendEmailForgotPassword(dialer, config.SMTP.Email, value.Email, value.Token); err != nil {
|
||||
log.Fatalf("failed to send email: %s\n", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
@ -29,7 +29,10 @@ type UserService interface {
|
||||
CreateUser(ctx context.Context, params UserCreateParams) (*models.UserDTO, error)
|
||||
AuthenticateUser(ctx context.Context, login, password string) (string, error)
|
||||
ValidateToken(ctx context.Context, tokenStr string) (*models.UserDTO, error)
|
||||
HelpPasswordForgot(ctx context.Context, userId string) error
|
||||
|
||||
ForgotPassword(ctx context.Context, userId string) error
|
||||
ChangePassword(ctx context.Context, userId, oldPassword, newPassword string) error
|
||||
ChangePasswordWithToken(ctx context.Context, userId, actionToken, newPassword string) error
|
||||
}
|
||||
|
||||
func NewUserService(deps UserServiceDeps) UserService {
|
||||
@ -85,8 +88,6 @@ func (u *userService) CreateUser(ctx context.Context, params UserCreateParams) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u.deps.EventRepo.SendEmailForgotPassword(ctx, user.Email, "123")
|
||||
|
||||
u.deps.UserCache.Set(result.Id, *result, cache.Expiration{Ttl: userCacheTtl})
|
||||
|
||||
return result, nil
|
||||
@ -116,7 +117,7 @@ func (u *userService) AuthenticateUser(ctx context.Context, email, password stri
|
||||
return jwt, nil
|
||||
}
|
||||
|
||||
func (u *userService) HelpPasswordForgot(ctx context.Context, userId string) error {
|
||||
func (u *userService) ForgotPassword(ctx context.Context, userId string) error {
|
||||
user, err := u.getUserById(ctx, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -138,13 +139,13 @@ func (u *userService) HelpPasswordForgot(ctx context.Context, userId string) err
|
||||
return u.deps.EventRepo.SendEmailForgotPassword(ctx, user.Email, actionToken.Value)
|
||||
}
|
||||
|
||||
func (u *userService) ChangePasswordForgot(ctx context.Context, userId, newPassword, accessCode string) error {
|
||||
func (u *userService) ChangePasswordWithToken(ctx context.Context, userId, actionToken, newPassword string) error {
|
||||
user, err := u.getUserById(ctx, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
code, err := u.deps.ActionTokenRepo.PopActionToken(ctx, userId, accessCode, models.ActionTokenTargetForgotPassword)
|
||||
code, err := u.deps.ActionTokenRepo.PopActionToken(ctx, userId, actionToken, models.ActionTokenTargetForgotPassword)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -64,7 +64,7 @@ func New(opts NewServerOpts) *Server {
|
||||
dummyGroup.GET("", handlers.NewDummyHandler())
|
||||
dummyGroup.POST("/forgot-password", func(c *gin.Context) {
|
||||
user := utils.GetUserFromRequest(c)
|
||||
opts.UserService.HelpPasswordForgot(c, user.Id)
|
||||
opts.UserService.ForgotPassword(c, user.Id)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user