SetupRouter 设置路由
(searchService *service.SearchService)
| 10 | |
| 11 | // SetupRouter 设置路由 |
| 12 | func SetupRouter(searchService *service.SearchService) *gin.Engine { |
| 13 | // 设置搜索服务 |
| 14 | SetSearchService(searchService) |
| 15 | |
| 16 | // 设置为生产模式 |
| 17 | gin.SetMode(gin.ReleaseMode) |
| 18 | |
| 19 | // 创建默认路由 |
| 20 | r := gin.Default() |
| 21 | |
| 22 | // 添加中间件 |
| 23 | r.Use(CORSMiddleware()) |
| 24 | r.Use(LoggerMiddleware()) |
| 25 | r.Use(util.GzipMiddleware()) // 添加压缩中间件 |
| 26 | r.Use(AuthMiddleware()) // 添加认证中间件 |
| 27 | |
| 28 | // 定义API路由组 |
| 29 | api := r.Group("/api") |
| 30 | { |
| 31 | // 认证接口(不需要认证,由中间件公开路径处理) |
| 32 | auth := api.Group("/auth") |
| 33 | { |
| 34 | auth.POST("/login", LoginHandler) |
| 35 | auth.POST("/verify", VerifyHandler) |
| 36 | auth.POST("/logout", LogoutHandler) |
| 37 | } |
| 38 | |
| 39 | // 搜索接口 - 支持POST和GET两种方式 |
| 40 | api.POST("/search", SearchHandler) |
| 41 | api.GET("/search", SearchHandler) // 添加GET方式支持 |
| 42 | |
| 43 | // 健康检查接口 |
| 44 | api.GET("/health", func(c *gin.Context) { |
| 45 | // 根据配置决定是否返回插件信息 |
| 46 | pluginCount := 0 |
| 47 | pluginNames := []string{} |
| 48 | pluginsEnabled := config.AppConfig.AsyncPluginEnabled |
| 49 | |
| 50 | if pluginsEnabled && searchService != nil && searchService.GetPluginManager() != nil { |
| 51 | plugins := searchService.GetPluginManager().GetPlugins() |
| 52 | pluginCount = len(plugins) |
| 53 | for _, p := range plugins { |
| 54 | pluginNames = append(pluginNames, p.Name()) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // 获取频道信息 |
| 59 | channels := config.AppConfig.DefaultChannels |
| 60 | channelsCount := len(channels) |
| 61 | |
| 62 | response := gin.H{ |
| 63 | "status": "ok", |
| 64 | "auth_enabled": config.AppConfig.AuthEnabled, // 添加认证状态 |
| 65 | "plugins_enabled": pluginsEnabled, |
| 66 | "channels": channels, |
| 67 | "channels_count": channelsCount, |
| 68 | } |
| 69 |
no test coverage detected