Skip to content

Commit

Permalink
feat: Support send code with email. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
IRONICBo authored Jun 26, 2023
1 parent 29190f5 commit acf9c9e
Show file tree
Hide file tree
Showing 24 changed files with 492 additions and 21 deletions.
9 changes: 8 additions & 1 deletion server/config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ minio:
secret_access_key: test123123
bucket: openkf
app_bucket: openkf
location: us-east-1
location: us-east-1

email:
host: # smtp address
port: 25
from: # email address
nickname: openkf
password: # smtp password
9 changes: 8 additions & 1 deletion server/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ minio:
secret_access_key: test123123
bucket: openkf
app_bucket: openkf
location: us-east-1
location: us-east-1

email:
host: # smtp address
port: 25
from: # email address
nickname: openkf
password: # smtp password
71 changes: 70 additions & 1 deletion server/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 70 additions & 1 deletion server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,74 @@
"info": {
"contact": {}
},
"paths": {}
"paths": {
"/api/v1/email/code": {
"post": {
"description": "Use email to send verification code",
"produces": [
"application/json"
],
"tags": [
"mail"
],
"summary": "SendCode",
"parameters": [
{
"description": "Email address",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/param.SendToParams"
}
}
],
"responses": {
"200": {
"description": "Send success",
"schema": {
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"msg": {
"type": "string"
}
}
}
]
}
}
}
}
}
},
"definitions": {
"param.SendToParams": {
"type": "object",
"required": [
"email"
],
"properties": {
"email": {
"type": "string"
}
}
},
"response.Response": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"data": {},
"msg": {
"type": "string"
}
}
}
}
}
43 changes: 42 additions & 1 deletion server/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
definitions:
param.SendToParams:
properties:
email:
type: string
required:
- email
type: object
response.Response:
properties:
code:
type: integer
data: {}
msg:
type: string
type: object
info:
contact: {}
paths: {}
paths:
/api/v1/email/code:
post:
description: Use email to send verification code
parameters:
- description: Email address
in: body
name: data
required: true
schema:
$ref: '#/definitions/param.SendToParams'
produces:
- application/json
responses:
"200":
description: Send success
schema:
allOf:
- $ref: '#/definitions/response.Response'
- properties:
msg:
type: string
type: object
summary: SendCode
tags:
- mail
swagger: "2.0"
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/go-redis/redis/v8 v8.11.5
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/minio/minio-go/v7 v7.0.57
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down
5 changes: 5 additions & 0 deletions server/internal/api/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright © 2023 OpenIMSDK open source community. All rights reserved.
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.

package api
1 change: 0 additions & 1 deletion server/internal/api/auth/auth.go

This file was deleted.

41 changes: 41 additions & 0 deletions server/internal/api/mail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2023 OpenIMSDK open source community. All rights reserved.
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.

package api

import (
"github.com/OpenIMSDK/OpenKF/server/internal/common"
"github.com/OpenIMSDK/OpenKF/server/internal/common/response"
"github.com/OpenIMSDK/OpenKF/server/internal/param"
"github.com/OpenIMSDK/OpenKF/server/internal/service"
"github.com/OpenIMSDK/OpenKF/server/pkg/log"
"github.com/gin-gonic/gin"
)

// SendCode
// @Tags mail
// @Summary SendCode
// @Description Use email to send verification code
// @Produce application/json
// @Param data body param.SendToParams true "Email address"
// @Success 200 {object} response.Response{msg=string} "Success"
// @Router /api/v1/email/code [post]
func SendCode(c *gin.Context) {
var params param.SendToParams
err := c.ShouldBindJSON(&params)
if err != nil {
response.FailWithCode(common.INVALID_PARAMS, c)
return
}

svc := service.NewServiceWithGin(c)
err = svc.SendCode(params.Email)
if err != nil {
log.Debug("SendCode error: ", err)
response.FailWithCode(common.ERROR, c)
return
}

response.Success(c)
}
51 changes: 51 additions & 0 deletions server/internal/client/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,54 @@
// you may not use this file except in compliance with the License.

package client

import (
"fmt"
"net/smtp"

"github.com/OpenIMSDK/OpenKF/server/internal/config"
"github.com/jordan-wright/email"
)

// todo: use email connection pool to reduce the cost of creating a connection
// link: https://github.com/jordan-wright/email#a-pool-of-reusable-connections

func SendEmail(to string, subject string, body string) error {
email := email.NewEmail()

email.From = fmt.Sprintf("%s<%s>", config.Config.Email.Nickname, config.Config.Email.From)
email.To = []string{to}
email.Subject = subject
email.Text = []byte(body)

if err := email.Send(
fmt.Sprintf("%s:%d", config.Config.Email.Host, config.Config.Email.Port),
smtp.PlainAuth(
"", config.Config.Email.From, config.Config.Email.Password, config.Config.Email.Host,
),
); err != nil {
return err
}

return nil
}

func SendHtmlEmail(to string, subject string, html string) error {
email := email.NewEmail()

email.From = config.Config.Email.From
email.To = []string{to}
email.Subject = subject
email.HTML = []byte(html)

if err := email.Send(
fmt.Sprintf("%s:%d", config.Config.Email.Host, config.Config.Email.Port),
smtp.PlainAuth(
"", config.Config.Email.From, config.Config.Email.Password, config.Config.Email.Host,
),
); err != nil {
return err
}

return nil
}
13 changes: 6 additions & 7 deletions server/internal/client/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import (
var _minioClient *minio.Client
var _bucket string

func init() {
endpoint := fmt.Sprintf("%s:%s", config.GetString("minio.ip"),
config.GetString("minio.port"))
accessKeyID := config.GetString("minio.access_key_id")
secretAccessKey := config.GetString("minio.secret_access_key")
location := config.GetString("minio.location")
_bucket = config.GetString("minio.bucket")
func InitMinio() {
endpoint := fmt.Sprintf("%s:%d", config.Config.Minio.Ip, config.Config.Minio.Port)
accessKeyID := config.Config.Minio.AccessKeyId
secretAccessKey := config.Config.Minio.SecretAccessKey
location := config.Config.Minio.Location
_bucket = config.Config.Minio.Bucket

// Initialize _minioClient
_minioClient, err := minio.New(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.

package e
package common

const (
SUCCESS = 200
Expand Down
2 changes: 1 addition & 1 deletion server/internal/e/msg.go → server/internal/common/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.

package e
package common

var Msg = map[int]string{
SUCCESS: "success",
Expand Down
Loading

0 comments on commit acf9c9e

Please sign in to comment.