Compare commits
2 Commits
feature/em
...
test
| Author | SHA1 | Date | |
|---|---|---|---|
| ea3f3fd482 | |||
| a29936f31c |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,7 +7,5 @@
|
|||||||
texpixel
|
texpixel
|
||||||
/vendor
|
/vendor
|
||||||
|
|
||||||
doc_ai
|
|
||||||
document_ai
|
|
||||||
dev_deploy.sh
|
dev_deploy.sh
|
||||||
speed_take.sh
|
speed_take.sh
|
||||||
@@ -43,7 +43,7 @@ func SetupRouter(engine *gin.RouterGroup) {
|
|||||||
userRouter := v1.Group("/user")
|
userRouter := v1.Group("/user")
|
||||||
{
|
{
|
||||||
userRouter.POST("/sms", userEndpoint.SendVerificationCode)
|
userRouter.POST("/sms", userEndpoint.SendVerificationCode)
|
||||||
userRouter.POST("/email/code", userEndpoint.SendEmailVerifyCode)
|
userRouter.POST("/email/code", userEndpoint.SendEmailVerificationCode)
|
||||||
userRouter.POST("/register", userEndpoint.RegisterByEmail)
|
userRouter.POST("/register", userEndpoint.RegisterByEmail)
|
||||||
userRouter.POST("/login", userEndpoint.LoginByEmail)
|
userRouter.POST("/login", userEndpoint.LoginByEmail)
|
||||||
userRouter.GET("/oauth/google/url", userEndpoint.GetGoogleOAuthUrl)
|
userRouter.GET("/oauth/google/url", userEndpoint.GetGoogleOAuthUrl)
|
||||||
|
|||||||
@@ -106,24 +106,24 @@ func (h *UserEndpoint) GetUserInfo(ctx *gin.Context) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *UserEndpoint) SendEmailVerifyCode(ctx *gin.Context) {
|
func (h *UserEndpoint) SendEmailVerificationCode(ctx *gin.Context) {
|
||||||
req := model.EmailVerifyCodeRequest{}
|
req := model.EmailCodeSendRequest{}
|
||||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeParamError, common.CodeParamErrorMsg))
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeParamError, common.CodeParamErrorMsg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.userService.SendEmailVerifyCode(ctx, req.Email); err != nil {
|
if err := h.userService.SendEmailCode(ctx, req.Email); err != nil {
|
||||||
if bizErr, ok := err.(*common.BusinessError); ok {
|
if bizErr, ok := err.(*common.BusinessError); ok {
|
||||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "发送邮件验证码失败", "error", err)
|
log.Error(ctx, "func", "SendEmailVerificationCode", "msg", "发送邮箱验证码失败", "error", err)
|
||||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeSystemError, common.CodeSystemErrorMsg))
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeSystemError, common.CodeSystemErrorMsg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.EmailVerifyCodeResponse{}))
|
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.EmailCodeSendResponse{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
|
func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
|
||||||
@@ -133,7 +133,7 @@ func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
uid, err := h.userService.RegisterByEmail(ctx, req.Email, req.Password, req.VerifyCode)
|
uid, err := h.userService.RegisterByEmail(ctx, req.Email, req.Password, req.Code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if bizErr, ok := err.(*common.BusinessError); ok {
|
if bizErr, ok := err.(*common.BusinessError); ok {
|
||||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ database:
|
|||||||
host: localhost
|
host: localhost
|
||||||
port: 3006
|
port: 3006
|
||||||
username: root
|
username: root
|
||||||
password: root123
|
password: texpixel#pwd123!
|
||||||
dbname: doc_ai
|
dbname: doc_ai
|
||||||
max_idle: 10
|
max_idle: 10
|
||||||
max_open: 100
|
max_open: 100
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
addr: localhost:6079
|
addr: localhost:6079
|
||||||
password: redis123
|
password: yoge@123321!
|
||||||
db: 0
|
db: 0
|
||||||
|
|
||||||
limit:
|
limit:
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
git push origin test
|
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||||
|
|
||||||
ssh ubuntu << 'ENDSSH'
|
git push origin "$BRANCH"
|
||||||
cd /home/yoge/Dev/doc_ai_backed
|
|
||||||
git checkout test
|
ssh ubuntu "
|
||||||
git pull origin test
|
cd /home/yoge/Dev/doc_ai_backed &&
|
||||||
docker compose -f docker-compose.infra.yml up -d
|
git checkout $BRANCH &&
|
||||||
docker compose down
|
git pull origin $BRANCH &&
|
||||||
docker image rm doc_ai_backed-doc_ai:latest
|
docker compose -f docker-compose.infra.yml up -d --no-recreate || true &&
|
||||||
|
docker compose down &&
|
||||||
|
docker image rm doc_ai_backed-doc_ai:latest || true &&
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
ENDSSH
|
"
|
||||||
@@ -1,68 +1,10 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
REGISTRY="crpi-8s2ierii2xan4klg.cn-beijing.personal.cr.aliyuncs.com/texpixel/doc_ai_backend"
|
docker build -t crpi-8s2ierii2xan4klg.cn-beijing.personal.cr.aliyuncs.com/texpixel/doc_ai_backend:latest . && docker push crpi-8s2ierii2xan4klg.cn-beijing.personal.cr.aliyuncs.com/texpixel/doc_ai_backend:latest
|
||||||
BUILD_HOST="ubuntu"
|
|
||||||
BUILD_DIR="~/Dev/doc_ai_backed"
|
|
||||||
|
|
||||||
# --- Guard: must be on main/master ---
|
ssh ecs << 'ENDSSH'
|
||||||
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
docker stop doc_ai doc_ai_backend 2>/dev/null || true
|
||||||
if [[ "${BRANCH}" != "main" && "${BRANCH}" != "master" ]]; then
|
docker rm doc_ai doc_ai_backend 2>/dev/null || true
|
||||||
echo "ERROR: must be on main or master branch (current: ${BRANCH})"
|
docker pull crpi-8s2ierii2xan4klg.cn-beijing.personal.cr.aliyuncs.com/texpixel/doc_ai_backend:latest
|
||||||
exit 1
|
docker run -d --name doc_ai -p 8024:8024 --restart unless-stopped crpi-8s2ierii2xan4klg.cn-beijing.personal.cr.aliyuncs.com/texpixel/doc_ai_backend:latest -env=prod
|
||||||
fi
|
ENDSSH
|
||||||
|
|
||||||
VERSION=$(git rev-parse --short HEAD)
|
|
||||||
IMAGE_VERSIONED="${REGISTRY}:${VERSION}"
|
|
||||||
IMAGE_LATEST="${REGISTRY}:latest"
|
|
||||||
|
|
||||||
echo "==> [1/3] Pulling latest code on Ubuntu"
|
|
||||||
ssh ${BUILD_HOST} "
|
|
||||||
set -e
|
|
||||||
cd ${BUILD_DIR}
|
|
||||||
git fetch origin
|
|
||||||
git checkout master 2>/dev/null || git checkout main
|
|
||||||
git pull
|
|
||||||
"
|
|
||||||
|
|
||||||
echo "==> [2/3] Building & pushing image on Ubuntu"
|
|
||||||
ssh ${BUILD_HOST} "
|
|
||||||
set -e
|
|
||||||
cd ${BUILD_DIR}
|
|
||||||
docker build --platform linux/amd64 \
|
|
||||||
-t ${IMAGE_VERSIONED} \
|
|
||||||
-t ${IMAGE_LATEST} \
|
|
||||||
.
|
|
||||||
docker push ${IMAGE_VERSIONED}
|
|
||||||
docker push ${IMAGE_LATEST}
|
|
||||||
docker rmi ${IMAGE_VERSIONED} ${IMAGE_LATEST} 2>/dev/null || true
|
|
||||||
"
|
|
||||||
|
|
||||||
echo "==> [3/3] Deploying on ECS"
|
|
||||||
ssh ecs "
|
|
||||||
set -e
|
|
||||||
echo '--- Pulling image'
|
|
||||||
docker pull ${IMAGE_VERSIONED}
|
|
||||||
|
|
||||||
echo '--- Stopping old container'
|
|
||||||
docker stop doc_ai 2>/dev/null || true
|
|
||||||
docker rm doc_ai 2>/dev/null || true
|
|
||||||
|
|
||||||
echo '--- Starting new container'
|
|
||||||
docker run -d \
|
|
||||||
--name doc_ai \
|
|
||||||
-p 8024:8024 \
|
|
||||||
--restart unless-stopped \
|
|
||||||
${IMAGE_VERSIONED} \
|
|
||||||
-env=prod
|
|
||||||
|
|
||||||
echo '--- Removing old doc_ai images (keeping current)'
|
|
||||||
docker images --format '{{.Repository}}:{{.Tag}} {{.ID}}' \
|
|
||||||
| grep '^${REGISTRY}' \
|
|
||||||
| grep -v ':${VERSION}' \
|
|
||||||
| grep -v ':latest' \
|
|
||||||
| awk '{print \$2}' \
|
|
||||||
| xargs -r docker rmi || true
|
|
||||||
"
|
|
||||||
|
|
||||||
echo "==> Done. Running version: ${VERSION}"
|
|
||||||
@@ -23,16 +23,16 @@ type UserInfoResponse struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailVerifyCodeRequest struct {
|
type EmailCodeSendRequest struct {
|
||||||
Email string `json:"email" binding:"required,email"`
|
Email string `json:"email" binding:"required,email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailVerifyCodeResponse struct{}
|
type EmailCodeSendResponse struct{}
|
||||||
|
|
||||||
type EmailRegisterRequest struct {
|
type EmailRegisterRequest struct {
|
||||||
Email string `json:"email" binding:"required,email"`
|
Email string `json:"email" binding:"required,email"`
|
||||||
Password string `json:"password" binding:"required,min=6"`
|
Password string `json:"password" binding:"required,min=6"`
|
||||||
VerifyCode string `json:"verify_code" binding:"required"`
|
Code string `json:"code" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmailRegisterResponse struct {
|
type EmailRegisterResponse struct {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.com/texpixel/document_ai/internal/model/task"
|
"gitea.com/texpixel/document_ai/internal/model/task"
|
||||||
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
||||||
|
|||||||
@@ -116,10 +116,10 @@ func (svc *UserService) GetUserInfo(ctx context.Context, uid int64) (*dao.User,
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *UserService) SendEmailVerifyCode(ctx context.Context, emailAddr string) error {
|
func (svc *UserService) SendEmailCode(ctx context.Context, emailAddr string) error {
|
||||||
limit, err := cache.GetUserSendEmailLimit(ctx, emailAddr)
|
limit, err := cache.GetUserSendEmailLimit(ctx, emailAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "get send email limit error", "error", err)
|
log.Error(ctx, "func", "SendEmailCode", "msg", "get send email limit error", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if limit >= cache.UserSendEmailLimitCount {
|
if limit >= cache.UserSendEmailLimitCount {
|
||||||
@@ -128,38 +128,33 @@ func (svc *UserService) SendEmailVerifyCode(ctx context.Context, emailAddr strin
|
|||||||
|
|
||||||
code := fmt.Sprintf("%06d", rand.Intn(1000000))
|
code := fmt.Sprintf("%06d", rand.Intn(1000000))
|
||||||
|
|
||||||
subject := "Your verification code"
|
subject := "TexPixel 邮箱验证码"
|
||||||
body := fmt.Sprintf("Your verification code is: %s\nIt will expire in 10 minutes.", code)
|
body := fmt.Sprintf(`<p>您的验证码为:<strong>%s</strong>,10分钟内有效,请勿泄露。</p>`, code)
|
||||||
if err := email.Send(ctx, emailAddr, subject, body); err != nil {
|
if err = email.Send(ctx, emailAddr, subject, body); err != nil {
|
||||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "send email error", "error", err)
|
log.Error(ctx, "func", "SendEmailCode", "msg", "send email error", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cacheErr := cache.SetUserEmailCode(ctx, emailAddr, code); cacheErr != nil {
|
if cacheErr := cache.SetUserEmailCode(ctx, emailAddr, code); cacheErr != nil {
|
||||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "set email code error", "error", cacheErr)
|
log.Error(ctx, "func", "SendEmailCode", "msg", "set email code error", "error", cacheErr)
|
||||||
}
|
}
|
||||||
if cacheErr := cache.SetUserSendEmailLimit(ctx, emailAddr); cacheErr != nil {
|
if cacheErr := cache.SetUserSendEmailLimit(ctx, emailAddr); cacheErr != nil {
|
||||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "set send email limit error", "error", cacheErr)
|
log.Error(ctx, "func", "SendEmailCode", "msg", "set send email limit error", "error", cacheErr)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc *UserService) RegisterByEmail(ctx context.Context, emailAddr, password, verifyCode string) (uid int64, err error) {
|
func (svc *UserService) RegisterByEmail(ctx context.Context, emailAddr, password, code string) (uid int64, err error) {
|
||||||
storedCode, err := cache.GetUserEmailCode(ctx, emailAddr)
|
storedCode, err := cache.GetUserEmailCode(ctx, emailAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "get email code error", "error", err)
|
log.Error(ctx, "func", "RegisterByEmail", "msg", "get email code error", "error", err)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
if storedCode == "" || storedCode != verifyCode {
|
if storedCode == "" || storedCode != code {
|
||||||
|
log.Warn(ctx, "func", "RegisterByEmail", "msg", "invalid email code", "email", emailAddr)
|
||||||
return 0, common.ErrEmailCodeError
|
return 0, common.ErrEmailCodeError
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = cache.DeleteUserEmailCode(ctx, emailAddr)
|
|
||||||
|
|
||||||
return svc.registerByEmailInternal(ctx, emailAddr, password)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (svc *UserService) registerByEmailInternal(ctx context.Context, emailAddr, password string) (uid int64, err error) {
|
|
||||||
existingUser, err := svc.userDao.GetByEmail(dao.DB.WithContext(ctx), emailAddr)
|
existingUser, err := svc.userDao.GetByEmail(dao.DB.WithContext(ctx), emailAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "get user by email error", "error", err)
|
log.Error(ctx, "func", "RegisterByEmail", "msg", "get user by email error", "error", err)
|
||||||
@@ -180,12 +175,15 @@ func (svc *UserService) registerByEmailInternal(ctx context.Context, emailAddr,
|
|||||||
Email: emailAddr,
|
Email: emailAddr,
|
||||||
Password: string(hashedPassword),
|
Password: string(hashedPassword),
|
||||||
}
|
}
|
||||||
err = svc.userDao.Create(dao.DB.WithContext(ctx), user)
|
if err = svc.userDao.Create(dao.DB.WithContext(ctx), user); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Error(ctx, "func", "RegisterByEmail", "msg", "create user error", "error", err)
|
log.Error(ctx, "func", "RegisterByEmail", "msg", "create user error", "error", err)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cacheErr := cache.DeleteUserEmailCode(ctx, emailAddr); cacheErr != nil {
|
||||||
|
log.Error(ctx, "func", "RegisterByEmail", "msg", "delete email code error", "error", cacheErr)
|
||||||
|
}
|
||||||
|
|
||||||
return user.ID, nil
|
return user.ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
internal/storage/cache/user.go
vendored
13
internal/storage/cache/user.go
vendored
@@ -88,6 +88,10 @@ func SetUserEmailCode(ctx context.Context, email, code string) error {
|
|||||||
return RedisClient.Set(ctx, fmt.Sprintf(UserEmailCodePrefix, email), code, UserEmailCodeTTL).Err()
|
return RedisClient.Set(ctx, fmt.Sprintf(UserEmailCodePrefix, email), code, UserEmailCodeTTL).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteUserEmailCode(ctx context.Context, email string) error {
|
||||||
|
return RedisClient.Del(ctx, fmt.Sprintf(UserEmailCodePrefix, email)).Err()
|
||||||
|
}
|
||||||
|
|
||||||
func GetUserSendEmailLimit(ctx context.Context, email string) (int, error) {
|
func GetUserSendEmailLimit(ctx context.Context, email string) (int, error) {
|
||||||
limit, err := RedisClient.Get(ctx, fmt.Sprintf(UserSendEmailLimit, email)).Result()
|
limit, err := RedisClient.Get(ctx, fmt.Sprintf(UserSendEmailLimit, email)).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -100,16 +104,13 @@ func GetUserSendEmailLimit(ctx context.Context, email string) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetUserSendEmailLimit(ctx context.Context, email string) error {
|
func SetUserSendEmailLimit(ctx context.Context, email string) error {
|
||||||
count, err := RedisClient.Incr(ctx, fmt.Sprintf(UserSendEmailLimit, email)).Result()
|
key := fmt.Sprintf(UserSendEmailLimit, email)
|
||||||
|
count, err := RedisClient.Incr(ctx, key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if count > UserSendEmailLimitCount {
|
if count > UserSendEmailLimitCount {
|
||||||
return errors.New("send email limit")
|
return errors.New("send email limit")
|
||||||
}
|
}
|
||||||
return RedisClient.Expire(ctx, fmt.Sprintf(UserSendEmailLimit, email), UserSendEmailLimitTTL).Err()
|
return RedisClient.Expire(ctx, key, UserSendEmailLimitTTL).Err()
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteUserEmailCode(ctx context.Context, email string) error {
|
|
||||||
return RedisClient.Del(ctx, fmt.Sprintf(UserEmailCodePrefix, email)).Err()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ const (
|
|||||||
CodeEmailExistsMsg = "email already registered"
|
CodeEmailExistsMsg = "email already registered"
|
||||||
CodeEmailNotFoundMsg = "email not found"
|
CodeEmailNotFoundMsg = "email not found"
|
||||||
CodePasswordMismatchMsg = "password mismatch"
|
CodePasswordMismatchMsg = "password mismatch"
|
||||||
CodeEmailCodeErrorMsg = "email verify code error"
|
CodeEmailCodeErrorMsg = "email code error"
|
||||||
CodeEmailSendLimitMsg = "email send limit reached"
|
CodeEmailSendLimitMsg = "email send limit reached"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user