feat: build core architecture & implement user auth modules 🚀 feat: 搭建核心架构并实现用户认证模块 🚀 Framework Migration: Switched from Hertz to Gin, providing a more idiomatic and lightweight web foundation. ⚡ 框架迁移:从 Hertz 切换至 Gin,构建了更符合 Go 惯例且轻量级的 Web 基础。⚡ Architectural Overhaul: Refactored the 3-layer architecture from global-variable-based calls to Explicit Dependency Injection (DI) via New... factory functions. This significantly improves testability and decoupling. 🏗️ 架构重构:将三层架构从基于“全局变量”的调用重构为通过 New... 工厂函数实现的显式依赖注入 (DI)。这大幅提升了代码的可测试性与解耦程度。🏗️ User Auth: Completed and tested Register, Login, and Token Refresh APIs with robust error handling and Bcrypt password hashing. 🔐 用户认证:完成了注册、登录与 Token 刷新接口并通过测试,包含健壮的错误处理与 Bcrypt 密码哈希加密。🔐 Config Management: Integrated Viper for centralized, environment-aware configuration management. ⚙️ 配置管理:集成了 Viper,实现了中心化且具备环境感知能力的配置管理。⚙️ DevOps & Docs: Added docker-compose.yml for seamless MySQL 8.0 & environment setup. 🐳 Updated README.md with corrections for mistakes in image quoting and formats. 📝 运维与文档: 新增 docker-compose.yml,实现 MySQL 8.0 环境的一键启动。🐳 更新 README.md,修改了一些图片引用和格式上小错误。📝
98 lines
2.7 KiB
Go
98 lines
2.7 KiB
Go
// Package api 定义API接口层
|
||
// 包含所有对外暴露的HTTP接口定义
|
||
package api
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/smartflow/backend/model"
|
||
"github.com/smartflow/backend/respond"
|
||
"github.com/smartflow/backend/service"
|
||
)
|
||
|
||
type UserHandler struct {
|
||
// 伸出手:准备接住 Service
|
||
svc *service.UserService
|
||
}
|
||
|
||
// NewUserHandler:组装 Handler 的“工厂”
|
||
func NewUserHandler(svc *service.UserService) *UserHandler {
|
||
return &UserHandler{
|
||
svc: svc, // 把传进来的 Service 揣进口袋里
|
||
}
|
||
}
|
||
|
||
// UserRegister 用户注册API
|
||
// 处理用户注册请求
|
||
func (api *UserHandler) UserRegister(c *gin.Context) {
|
||
var user model.UserRegisterRequest
|
||
err := c.ShouldBindJSON(&user)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
|
||
retUser, err := api.svc.UserRegister(user)
|
||
if err != nil {
|
||
switch {
|
||
case errors.Is(err, respond.InvalidName), errors.Is(err, respond.MissingParam),
|
||
errors.Is(err, respond.ParamTooLong): //如果是无效ID或者缺少参数的错误
|
||
c.JSON(http.StatusBadRequest, err)
|
||
return
|
||
default:
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||
return
|
||
}
|
||
}
|
||
|
||
c.JSON(http.StatusOK, respond.OKWithData(respond.Ok, retUser))
|
||
}
|
||
|
||
func (api *UserHandler) UserLogin(c *gin.Context) {
|
||
var req model.UserLoginRequest
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, respond.WrongParamType)
|
||
return
|
||
}
|
||
tokens, err := api.svc.UserLogin(&req)
|
||
if err != nil {
|
||
switch {
|
||
case errors.Is(err, respond.WrongName), errors.Is(err, respond.WrongPwd): //如果是无效ID或者缺少参数的错误
|
||
c.JSON(http.StatusBadRequest, err)
|
||
return
|
||
default:
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||
return
|
||
}
|
||
}
|
||
c.JSON(http.StatusOK, respond.OKWithData(respond.Ok, tokens))
|
||
}
|
||
|
||
func (api *UserHandler) RefreshTokenHandler(c *gin.Context) {
|
||
var requestBody struct {
|
||
RefreshToken string `json:"old_refresh_token"`
|
||
}
|
||
if err := c.ShouldBindJSON(&requestBody); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
if requestBody.RefreshToken == "" {
|
||
c.JSON(http.StatusBadRequest, respond.MissingParam)
|
||
}
|
||
tokens, err := api.svc.RefreshTokenHandler(requestBody.RefreshToken)
|
||
if err != nil {
|
||
switch {
|
||
case errors.Is(err, respond.InvalidRefreshToken), errors.Is(err, respond.InvalidClaims),
|
||
errors.Is(err, respond.InvalidTokenSingingMethod): //如果是无效刷新令牌或者无效claims或者无效签名方法
|
||
c.JSON(http.StatusBadRequest, err)
|
||
return
|
||
default:
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||
}
|
||
}
|
||
c.JSON(http.StatusOK, respond.OKWithData(respond.Ok, tokens))
|
||
}
|