init: taotie-api 项目初始化
This commit is contained in:
45
api/api.go
Normal file
45
api/api.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"taotie-api/api/middleware"
|
||||
v1 "taotie-api/api/v1"
|
||||
"taotie-api/api/wrap"
|
||||
"taotie-api/core"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
// 依赖注入节点
|
||||
var RouterProd = wire.NewSet(
|
||||
NewRouter,
|
||||
v1.NewUserApi,
|
||||
)
|
||||
|
||||
func NewRouter(cfg *core.Configuration, userApi *v1.UserApi) *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
// cors
|
||||
r.Use(middleware.Cors())
|
||||
|
||||
// 定义路由
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"message": "Hello, World!",
|
||||
})
|
||||
})
|
||||
|
||||
apiv1 := r.Group("/api/v1")
|
||||
{
|
||||
apiv1.POST("/login", wrap.Wrap(userApi.Login))
|
||||
apiv1.POST("/register", wrap.Wrap(userApi.Register))
|
||||
}
|
||||
|
||||
apiv1_user := apiv1.Group("/user")
|
||||
apiv1_user.Use(middleware.Auth(cfg))
|
||||
{
|
||||
apiv1_user.GET("/current", wrap.Wrap(userApi.GetCurrentUserInfo))
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
57
api/middleware/authMiddleware.go
Normal file
57
api/middleware/authMiddleware.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"taotie-api/common"
|
||||
"taotie-api/core"
|
||||
"taotie-api/utils/sctx"
|
||||
"taotie-api/utils/sjwt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Auth(cfg *core.Configuration) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.ErrSysValidationFailed.Info().Id,
|
||||
"msg": "缺少 Authorization 头",
|
||||
"ok": false,
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
parts := strings.SplitN(authHeader, " ", 2)
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.ErrSysValidationFailed.Info().Id,
|
||||
"msg": "Authorization 格式错误,应为 Bearer <token>",
|
||||
"ok": false,
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := sjwt.ParseToken(parts[1], cfg.JWT.SignString)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.ErrSysValidationFailed.Info().Id,
|
||||
"msg": "token 无效或已过期",
|
||||
"ok": false,
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
sctx.SetCurrentUser(c, &sctx.CurrentUser{
|
||||
UserId: claims.UserId,
|
||||
UserName: claims.UserName,
|
||||
TenantId: claims.TenantId,
|
||||
})
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
19
api/middleware/corsMiddleware.go
Normal file
19
api/middleware/corsMiddleware.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Cors() gin.HandlerFunc {
|
||||
return cors.New(cors.Config{
|
||||
AllowOrigins: []string{"*"}, // 允许所有来源(生产环境不建议使用)
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, // 允许的请求方法
|
||||
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"}, // 允许的请求头部
|
||||
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Authorization"}, // 允许客户端获取的响应头部
|
||||
AllowCredentials: true, // 允许携带 Cookie
|
||||
MaxAge: 12 * time.Hour, // 预检请求的缓存时间
|
||||
})
|
||||
}
|
||||
85
api/v1/userapi.go
Normal file
85
api/v1/userapi.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"taotie-api/common"
|
||||
"taotie-api/model/do/userdo"
|
||||
"taotie-api/model/dto/userdto"
|
||||
"taotie-api/service"
|
||||
"taotie-api/utils/sctx"
|
||||
)
|
||||
|
||||
type UserApi struct {
|
||||
userService *service.UserService
|
||||
}
|
||||
|
||||
func NewUserApi(userService *service.UserService) *UserApi {
|
||||
return &UserApi{userService}
|
||||
}
|
||||
|
||||
// Login 登录
|
||||
func (api *UserApi) Login(ctx context.Context, req *userdto.LoginReq) (*userdto.LoginRes, error) {
|
||||
out, err := api.userService.Login(ctx, &userdo.LoginIn{
|
||||
TenantNo: req.TenantNo,
|
||||
UserName: req.UserName,
|
||||
Password: req.Password,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 登录成功,返回 token
|
||||
tokenOut, err := api.userService.GenToken(ctx, &userdo.GenTokenIn{
|
||||
TenantNo: out.TenantId,
|
||||
UserId: out.UserId,
|
||||
UserName: out.UserName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &userdto.LoginRes{
|
||||
Token: tokenOut.Token,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Register 注册
|
||||
func (api *UserApi) Register(ctx context.Context, req *userdto.RegisterReq) (*userdto.RegisterRes, error) {
|
||||
// 创建租户和用户
|
||||
out, err := api.userService.Create(ctx, &userdo.CreateIn{
|
||||
TenantName: req.TenantName,
|
||||
UserName: req.UserName,
|
||||
Password: req.Password,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 自动登录,生成 token
|
||||
tokenOut, err := api.userService.GenToken(ctx, &userdo.GenTokenIn{
|
||||
TenantNo: out.TenantId,
|
||||
UserId: out.UserId,
|
||||
UserName: out.UserName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &userdto.RegisterRes{
|
||||
Token: tokenOut.Token,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetCurrentUserInfo 获取当前用户信息
|
||||
func (api *UserApi) GetCurrentUserInfo(ctx context.Context, req *userdto.GetCurrentUserInfoReq) (*userdto.GetCurrentUserInfoRes, error) {
|
||||
cuser := sctx.GetCurrentUser(ctx)
|
||||
if cuser == nil {
|
||||
return nil, common.ErrUserNotFound
|
||||
}
|
||||
|
||||
return &userdto.GetCurrentUserInfoRes{
|
||||
UserId: cuser.UserId,
|
||||
UserName: cuser.UserName,
|
||||
TenantId: cuser.TenantId,
|
||||
}, nil
|
||||
}
|
||||
110
api/wrap/wrap.go
Normal file
110
api/wrap/wrap.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package wrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"taotie-api/common"
|
||||
"time"
|
||||
|
||||
"github.com/duke-git/lancet/v2/xerror"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gookit/validate"
|
||||
)
|
||||
|
||||
func Wrap[R, P any](fn func(context.Context, *R) (*P, error)) func(c *gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
// defer func() {
|
||||
// if r := recover(); r != nil {
|
||||
// Err(c, errors.New(fmt.Sprintf("panic: %v", r)))
|
||||
// c.Abort()
|
||||
// }
|
||||
// }()
|
||||
|
||||
// 绑定请求参数
|
||||
var req = new(R)
|
||||
|
||||
// 检查请求结构体是否为空结构体
|
||||
reqType := reflect.TypeOf(req).Elem()
|
||||
isEmptyStruct := reqType.NumField() == 0
|
||||
|
||||
if !isEmptyStruct {
|
||||
if c.Request.ContentLength != 0 {
|
||||
// 绑定请求参数
|
||||
if err := c.ShouldBindJSON(req); err != nil {
|
||||
er := common.ErrSysValidationFailed.Wrap(err)
|
||||
Err(c, er)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 校验请求参数
|
||||
v := validate.New(req)
|
||||
if !v.Validate() {
|
||||
er := common.ErrSysValidationFailed.Wrap(v.Errors.OneError())
|
||||
Err(c, er)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 调用业务逻辑
|
||||
res, err := fn(c, req)
|
||||
if err != nil {
|
||||
Err(c, err)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
Ok(c, res)
|
||||
}
|
||||
}
|
||||
|
||||
type Resp struct {
|
||||
Code string `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data any `json:"data"`
|
||||
Ok bool `json:"ok"`
|
||||
Time int64 `json:"time"`
|
||||
}
|
||||
|
||||
func Ok(c *gin.Context, data any) {
|
||||
c.JSON(200, Resp{
|
||||
Code: common.ErrSysOk.Info().Id,
|
||||
Msg: common.ErrSysOk.Info().Message,
|
||||
Data: data,
|
||||
Ok: true,
|
||||
Time: time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
// Err 处理错误
|
||||
// // 注意⚠️:这里需要想想是不是所有的错误都要返回给 web 端
|
||||
func Err(c *gin.Context, err error) {
|
||||
// 返回给 web 端的错误
|
||||
var weberr = common.ErrSysInternal
|
||||
|
||||
// 提取最底层的错误
|
||||
causeErr := xerror.Unwrap(err)
|
||||
// 如果是自定义错误,解析错误,返回给 web 端
|
||||
if causeErr != nil {
|
||||
weberr = causeErr
|
||||
} else {
|
||||
slog.Error("【SRV-ERR】", "ERR", fmt.Errorf("%+v", err))
|
||||
}
|
||||
|
||||
// 系统内部错误的话,就不返回给 web 端,只记录。
|
||||
|
||||
// 记录日志
|
||||
// slog.Error("【WEB-ERR】", "ERR", weberr)
|
||||
// slog.Error("【SRV-ERR】", "ERR", fmt.Errorf("%+v", err))
|
||||
|
||||
c.JSON(200, Resp{
|
||||
Code: weberr.Info().Id,
|
||||
Msg: weberr.Info().Message,
|
||||
Ok: false,
|
||||
Time: time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user