startServer 启动Web服务器
()
| 136 | |
| 137 | // startServer 启动Web服务器 |
| 138 | func startServer() { |
| 139 | // 初始化插件管理器 |
| 140 | pluginManager := plugin.NewPluginManager() |
| 141 | |
| 142 | // 注册全局插件(根据配置过滤) |
| 143 | if config.AppConfig.AsyncPluginEnabled { |
| 144 | pluginManager.RegisterGlobalPluginsWithFilter(config.AppConfig.EnabledPlugins) |
| 145 | } |
| 146 | |
| 147 | // 更新默认并发数(如果插件被禁用则使用0) |
| 148 | pluginCount := 0 |
| 149 | if config.AppConfig.AsyncPluginEnabled { |
| 150 | pluginCount = len(pluginManager.GetPlugins()) |
| 151 | } |
| 152 | config.UpdateDefaultConcurrency(pluginCount) |
| 153 | |
| 154 | // 初始化搜索服务 |
| 155 | searchService := service.NewSearchService(pluginManager) |
| 156 | |
| 157 | // 设置路由 |
| 158 | router := api.SetupRouter(searchService) |
| 159 | |
| 160 | // 获取端口配置 |
| 161 | port := config.AppConfig.Port |
| 162 | |
| 163 | // 输出服务信息 |
| 164 | printServiceInfo(port, pluginManager) |
| 165 | |
| 166 | // 创建HTTP服务器 |
| 167 | srv := &http.Server{ |
| 168 | Addr: ":" + port, |
| 169 | Handler: router, |
| 170 | ReadTimeout: config.AppConfig.HTTPReadTimeout, |
| 171 | WriteTimeout: config.AppConfig.HTTPWriteTimeout, |
| 172 | IdleTimeout: config.AppConfig.HTTPIdleTimeout, |
| 173 | } |
| 174 | |
| 175 | // 创建通道来接收操作系统信号 |
| 176 | quit := make(chan os.Signal, 1) |
| 177 | signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) |
| 178 | |
| 179 | // 在单独的goroutine中启动服务器 |
| 180 | go func() { |
| 181 | // 如果设置了最大连接数,使用限制监听器 |
| 182 | if config.AppConfig.HTTPMaxConns > 0 { |
| 183 | // 创建监听器 |
| 184 | listener, err := net.Listen("tcp", srv.Addr) |
| 185 | if err != nil { |
| 186 | log.Fatalf("创建监听器失败: %v", err) |
| 187 | } |
| 188 | |
| 189 | // 创建限制连接数的监听器 |
| 190 | limitListener := netutil.LimitListener(listener, config.AppConfig.HTTPMaxConns) |
| 191 | |
| 192 | // 使用限制监听器启动服务器 |
| 193 | if err := srv.Serve(limitListener); err != nil && err != http.ErrServerClosed { |
| 194 | log.Fatalf("启动服务器失败: %v", err) |
| 195 | } |
no test coverage detected