mergeConfigFrom( __DIR__ . '/../Config/openapi.php', 'openapi' ); // 注册服务 $this->registerServices(); // 注册Handler $this->registerHandlers(); } /** * 启动服务 * * @return void */ public function boot() { // 注册路由 $this->registerRoutes(); // 注册中间件 $this->registerMiddleware(); // 注册事件监听器 $this->registerEventListeners(); // 注册命令 $this->registerCommands(); } /** * 注册服务 * * @return void */ protected function registerServices() { // 注册OpenAPI服务 $this->app->singleton('openapi.service', function () { return new \App\Module\OpenAPI\Services\OpenApiService(); }); // 注册认证服务 $this->app->singleton('openapi.auth', function ($app) { return new \App\Module\OpenAPI\Services\AuthService( $app->make('openapi.service') ); }); // 注册日志服务 $this->app->singleton('openapi.log', function () { return new \App\Module\OpenAPI\Services\LogService(); }); // 注册频率限制服务 $this->app->singleton('openapi.rate_limit', function () { return new \App\Module\OpenAPI\Services\RateLimitService(); }); // 注册权限范围服务 $this->app->singleton('openapi.scope', function () { return new \App\Module\OpenAPI\Services\ScopeService(); }); // 注册Webhook服务 $this->app->singleton('openapi.webhook', function () { return new \App\Module\OpenAPI\Services\WebhookService(); }); } /** * 注册Handler * * @return void */ protected function registerHandlers() { $handlerService = new \App\Module\OpenAPI\Services\HandlerRegistrationService(); $handlerService->registerHandlers(); } /** * 注册路由 * * @return void */ protected function registerRoutes() { // 使用路由注册服务来注册路由 $routeService = new \App\Module\OpenAPI\Services\RouteRegistrationService(); $routeService->registerAllRoutes(); // 注册后台管理路由(保留原有的后台路由文件) if (file_exists($adminRoutes = __DIR__ . '/../Routes/admin.php')) { Route::middleware(['web', 'admin']) ->prefix(config('admin.route.prefix', 'admin')) ->group($adminRoutes); } } /** * 注册中间件 * * @return void */ protected function registerMiddleware() { $router = $this->app['router']; // 注册中间件别名 $router->aliasMiddleware('openapi.auth', ApiAuthMiddleware::class); $router->aliasMiddleware('openapi.rate_limit', RateLimitMiddleware::class); $router->aliasMiddleware('openapi.scope', ScopeMiddleware::class); $router->aliasMiddleware('openapi.ip_whitelist', IpWhitelistMiddleware::class); // 注册中间件组 $router->middlewareGroup('openapi', [ 'openapi.auth', 'openapi.rate_limit', 'openapi.scope', 'openapi.ip_whitelist', ]); } /** * 注册事件监听器 * * @return void */ protected function registerEventListeners() { Event::listen( AppCreatedEvent::class, AppCreatedListener::class ); } /** * 注册命令 * * @return void */ protected function registerCommands() { if ($this->app->runningInConsole()) { $this->commands([ \App\Module\OpenAPI\Commands\GenerateApiKeyCommand::class, \App\Module\OpenAPI\Commands\CleanExpiredTokensCommand::class, \App\Module\OpenAPI\Commands\GenerateStatsCommand::class, \App\Module\OpenAPI\Commands\CleanDataCommand::class, ]); } } /** * 获取提供的服务 * * @return array */ public function provides() { return [ 'openapi.service', 'openapi.auth', 'openapi.log', 'openapi.rate_limit', 'openapi.scope', 'openapi.webhook', ]; } }