TODO: Email Valid Url

This commit is contained in:
eson 2023-07-24 19:43:56 +08:00
parent b43b5dbd59
commit a248c6cbeb
4 changed files with 26 additions and 16 deletions

View File

@ -29,7 +29,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodGet,
Path: "/api/auth/oauth2/login/register",
Path: "/api/auth/oauth2/register",
Handler: UserEmailRegisterHandler(serverCtx),
},
},

View File

@ -14,11 +14,12 @@ var EmailManager *EmailSender
// EmailSender
type EmailSender struct {
lock sync.Mutex
EmailTasks chan string // 处理email的队列
Auth smtp.Auth // 邮箱发送处理
FromEmail string // 发送的email, 公司email
emailSending map[string]*EmailTask // 正在发送的邮件
ResendTimeLimit time.Duration // 重发时间限制
EmailTasks chan string
Auth smtp.Auth
FromEmail string
emailSending map[string]*EmailTask
ResendTimeLimit time.Duration
semaphore chan struct{}
}
// EmailTask
@ -27,7 +28,6 @@ type EmailTask struct {
SendTime time.Time // 处理的任务时间
}
// ProcessEmailTasks 处理邮件队列
func (m *EmailSender) ProcessEmailTasks() {
for {
emailTarget, ok := <-m.EmailTasks
@ -49,12 +49,19 @@ func (m *EmailSender) ProcessEmailTasks() {
}
m.lock.Unlock()
content := []byte("Hello, this is a test email")
err := smtp.SendMail(emailTarget, m.Auth, m.FromEmail, []string{emailTarget}, content)
// Acquire a token
m.semaphore <- struct{}{}
go func() {
defer func() { <-m.semaphore }() // Release a token
content := RenderEmailTemplate("fusen", "http://www.baidu.com", "fusen@gmail.com", "mail-valid")
err := smtp.SendMail("smtp.gmail.com:587", m.Auth, m.FromEmail, []string{emailTarget}, content)
if err != nil {
log.Printf("Failed to send email to %s: %v\n", emailTarget, err)
m.Resend(emailTarget, content)
}
}()
}
}
@ -108,6 +115,7 @@ func init() {
FromEmail: "user@example.com",
emailSending: make(map[string]*EmailTask, 10),
ResendTimeLimit: time.Minute * 1,
semaphore: make(chan struct{}, 10), // Initialize semaphore with a capacity of 10
}
// Start processing email tasks
@ -137,7 +145,7 @@ Thanks,
{{.CompanyName}}
`
func RenderEmailTemplate(companyName, recipient, confirmationLink, senderName, senderTitle string) string {
func RenderEmailTemplate(companyName, confirmationLink, senderName, senderTitle string) []byte {
tmpl, err := template.New("email").Parse(emailTemplate)
if err != nil {
log.Fatal(err)
@ -156,5 +164,5 @@ func RenderEmailTemplate(companyName, recipient, confirmationLink, senderName, s
log.Fatal(err)
}
return result.String()
return result.Bytes()
}

View File

@ -158,6 +158,8 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
return resp.SetStatus(basic.CodeOAuthEmailErr)
}
EmailManager.EmailTasks <- req.Email // email进入队
return resp.SetStatus(basic.CodeOK)
}

View File

@ -20,7 +20,7 @@ service auth {
get /api/auth/oauth2/login/google(RequestGoogleLogin) returns (response);
@handler UserEmailRegisterHandler
get /api/auth/oauth2/login/register(RequestEmailRegister) returns (response);
get /api/auth/oauth2/register(RequestEmailRegister) returns (response);
}
// UserAddAddressHandler 用户登录请求结构