feat: 初始化配置中心项目

This commit is contained in:
2026-01-14 21:02:13 +08:00
commit 4436d3ee88
95 changed files with 8736 additions and 0 deletions
+19
View 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", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: false,
MaxAge: 12 * time.Hour,
})
}
+40
View File
@@ -0,0 +1,40 @@
package middleware
import (
"kenaito-conf/server/internal/model"
"kenaito-conf/server/internal/service"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func JWTAuth(authService *service.AuthService) gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "msg": "未登录"})
c.Abort()
return
}
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
userInfo, err := authService.ValidateToken(tokenString)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "msg": "登录已过期"})
c.Abort()
return
}
c.Set("userInfo", userInfo)
c.Next()
}
}
func GetUserInfo(c *gin.Context) *model.LoginUserInfo {
val, exists := c.Get("userInfo")
if !exists {
return nil
}
return val.(*model.LoginUserInfo)
}