You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.3 KiB
111 lines
3.3 KiB
package service |
|
|
|
import ( |
|
"fmt" |
|
"gofaster/internal/auth/repository" |
|
"reflect" |
|
"strings" |
|
|
|
"github.com/gin-gonic/gin" |
|
"go.uber.org/zap" |
|
) |
|
|
|
// EnhancedRouteSyncService 增强版路由同步服务 |
|
type EnhancedRouteSyncService struct { |
|
*RouteSyncService |
|
} |
|
|
|
// NewEnhancedRouteSyncService 创建增强版路由同步服务 |
|
func NewEnhancedRouteSyncService( |
|
routeMappingRepo *repository.RouteMappingRepository, |
|
resourceRepo repository.ResourceRepository, |
|
log *zap.Logger, |
|
) *EnhancedRouteSyncService { |
|
return &EnhancedRouteSyncService{ |
|
RouteSyncService: NewRouteSyncService(routeMappingRepo, resourceRepo, log), |
|
} |
|
} |
|
|
|
// generateDescriptionFromSwagger 从Swagger注释中生成描述 |
|
func (s *EnhancedRouteSyncService) generateDescriptionFromSwagger(method, path string, handler interface{}) string { |
|
// 使用反射获取handler的注释信息 |
|
handlerType := reflect.TypeOf(handler) |
|
if handlerType == nil { |
|
return s.generateDescription(method, path) |
|
} |
|
|
|
// 尝试从方法注释中提取@Summary信息 |
|
// 这里需要结合AST解析来获取注释,暂时使用简化版本 |
|
return s.generateDescription(method, path) |
|
} |
|
|
|
// generateEnhancedDescription 生成增强版描述 |
|
func (s *EnhancedRouteSyncService) generateEnhancedDescription(method, path string) string { |
|
// 定义更详细的描述映射 |
|
pathDescriptions := map[string]string{ |
|
"/auth/login": "用户登录", |
|
"/auth/logout": "用户登出", |
|
"/auth/captcha": "获取验证码", |
|
"/auth/userinfo": "获取用户信息", |
|
"/auth/change-password": "修改密码", |
|
"/auth/password-policy": "获取密码策略", |
|
"/auth/validate-password": "验证密码强度", |
|
"/auth/admin/users": "用户管理", |
|
"/auth/admin/users/:id": "用户详情操作", |
|
"/auth/roles": "角色管理", |
|
"/auth/roles/:id": "角色详情操作", |
|
"/auth/permissions": "权限管理", |
|
"/auth/permissions/:id": "权限详情操作", |
|
"/auth/resources": "资源管理", |
|
"/auth/resources/:id": "资源详情操作", |
|
} |
|
|
|
// 先尝试精确匹配 |
|
if desc, exists := pathDescriptions[path]; exists { |
|
return desc |
|
} |
|
|
|
// 尝试模式匹配 |
|
for pattern, desc := range pathDescriptions { |
|
if strings.Contains(path, strings.TrimSuffix(pattern, "/:id")) { |
|
switch method { |
|
case "GET": |
|
if strings.Contains(path, "/:id") { |
|
return fmt.Sprintf("获取%s详情", desc) |
|
} |
|
return fmt.Sprintf("获取%s列表", desc) |
|
case "POST": |
|
return fmt.Sprintf("创建%s", desc) |
|
case "PUT": |
|
return fmt.Sprintf("更新%s", desc) |
|
case "DELETE": |
|
return fmt.Sprintf("删除%s", desc) |
|
} |
|
} |
|
} |
|
|
|
// 回退到原始逻辑 |
|
return s.generateDescription(method, path) |
|
} |
|
|
|
// collectRoutesWithEnhancedDescription 收集路由信息并生成增强描述 |
|
func (s *EnhancedRouteSyncService) collectRoutesWithEnhancedDescription(router *gin.Engine) []RouteInfo { |
|
var routes []RouteInfo |
|
|
|
// 遍历所有注册的路由 |
|
for _, route := range router.Routes() { |
|
if route.Method != "" && route.Path != "" { |
|
module := s.extractModuleFromPath(route.Path) |
|
description := s.generateEnhancedDescription(route.Method, route.Path) |
|
|
|
routes = append(routes, RouteInfo{ |
|
Path: route.Path, |
|
Method: route.Method, |
|
Module: module, |
|
Description: description, |
|
}) |
|
} |
|
} |
|
|
|
return routes |
|
}
|
|
|