|
1 | 1 | import 'package:dart_frog/dart_frog.dart';
|
| 2 | +import 'package:ht_api/src/config/dependency_container.dart'; |
2 | 3 | import 'package:ht_api/src/middlewares/error_handler.dart';
|
| 4 | +import 'package:ht_api/src/rbac/permission_service.dart'; |
| 5 | +import 'package:ht_api/src/services/auth_service.dart'; |
| 6 | +import 'package:ht_api/src/services/auth_token_service.dart'; |
| 7 | +import 'package:ht_api/src/services/dashboard_summary_service.dart'; |
| 8 | +import 'package:ht_api/src/services/token_blacklist_service.dart'; |
| 9 | +import 'package:ht_api/src/services/user_preference_limit_service.dart'; |
| 10 | +import 'package:ht_api/src/services/verification_code_storage_service.dart'; |
| 11 | +import 'package:ht_data_repository/ht_data_repository.dart'; |
| 12 | +import 'package:ht_email_repository/ht_email_repository.dart'; |
| 13 | +import 'package:ht_shared/ht_shared.dart'; |
3 | 14 | import 'package:uuid/uuid.dart';
|
4 | 15 |
|
5 | 16 | // --- Request ID Wrapper ---
|
@@ -46,27 +57,104 @@ class RequestId {
|
46 | 57 |
|
47 | 58 | // --- Middleware Definition ---
|
48 | 59 | Handler middleware(Handler handler) {
|
49 |
| - // This is the root middleware chain for the entire API. |
50 |
| - // The order is important: |
51 |
| - // 1. Request ID: Assigns a unique ID to each request for tracing. |
52 |
| - // 2. Request Logger: Logs request and response details. |
53 |
| - // 3. Error Handler: Catches all errors and formats them into a standard |
54 |
| - // JSON response. |
| 60 | + // This is the root middleware for the entire API. It's responsible for |
| 61 | + // providing all shared dependencies to the request context. |
| 62 | + // The order of `.use()` calls is important: the last one in the chain |
| 63 | + // runs first. |
55 | 64 | return handler
|
| 65 | + // --- Core Middleware --- |
| 66 | + // These run after all dependencies have been provided. |
56 | 67 | .use(errorHandler())
|
57 | 68 | .use(requestLogger())
|
| 69 | + // --- Request ID Provider --- |
| 70 | + // This middleware provides a unique ID for each request for tracing. |
| 71 | + // It depends on the Uuid provider, so it must come after it. |
58 | 72 | .use((innerHandler) {
|
59 |
| - // This middleware reads the Uuid and provides the RequestId. |
60 |
| - // It must come after the Uuid provider in the chain. |
61 | 73 | return (context) {
|
62 |
| - // Read the Uuid instance provided from the previous middleware. |
63 | 74 | final uuid = context.read<Uuid>();
|
64 | 75 | final requestId = RequestId(uuid.v4());
|
65 | 76 | return innerHandler(context.provide<RequestId>(() => requestId));
|
66 | 77 | };
|
67 | 78 | })
|
| 79 | + // --- Dependency Providers --- |
| 80 | + // These providers inject all repositories and services into the context. |
| 81 | + // They read from the `DependencyContainer` which was populated at startup. |
| 82 | + // This is the first set of middleware to run for any request. |
| 83 | + .use(provider<Uuid>((_) => const Uuid())) |
68 | 84 | .use(
|
69 |
| - // This provider is last in the chain, so it runs first. |
70 |
| - provider<Uuid>((_) => const Uuid()), |
| 85 | + provider<HtDataRepository<Headline>>( |
| 86 | + (_) => DependencyContainer.instance.headlineRepository, |
| 87 | + ), |
| 88 | + ) |
| 89 | + .use( |
| 90 | + provider<HtDataRepository<Category>>( |
| 91 | + (_) => DependencyContainer.instance.categoryRepository, |
| 92 | + ), |
| 93 | + ) |
| 94 | + .use( |
| 95 | + provider<HtDataRepository<Source>>( |
| 96 | + (_) => DependencyContainer.instance.sourceRepository, |
| 97 | + ), |
| 98 | + ) |
| 99 | + .use( |
| 100 | + provider<HtDataRepository<Country>>( |
| 101 | + (_) => DependencyContainer.instance.countryRepository, |
| 102 | + ), |
| 103 | + ) |
| 104 | + .use( |
| 105 | + provider<HtDataRepository<User>>( |
| 106 | + (_) => DependencyContainer.instance.userRepository, |
| 107 | + ), |
| 108 | + ) |
| 109 | + .use( |
| 110 | + provider<HtDataRepository<UserAppSettings>>( |
| 111 | + (_) => DependencyContainer.instance.userAppSettingsRepository, |
| 112 | + ), |
| 113 | + ) |
| 114 | + .use( |
| 115 | + provider<HtDataRepository<UserContentPreferences>>( |
| 116 | + (_) => DependencyContainer.instance.userContentPreferencesRepository, |
| 117 | + ), |
| 118 | + ) |
| 119 | + .use( |
| 120 | + provider<HtDataRepository<AppConfig>>( |
| 121 | + (_) => DependencyContainer.instance.appConfigRepository, |
| 122 | + ), |
| 123 | + ) |
| 124 | + .use( |
| 125 | + provider<HtEmailRepository>( |
| 126 | + (_) => DependencyContainer.instance.emailRepository, |
| 127 | + ), |
| 128 | + ) |
| 129 | + .use( |
| 130 | + provider<TokenBlacklistService>( |
| 131 | + (_) => DependencyContainer.instance.tokenBlacklistService, |
| 132 | + ), |
| 133 | + ) |
| 134 | + .use( |
| 135 | + provider<AuthTokenService>( |
| 136 | + (_) => DependencyContainer.instance.authTokenService, |
| 137 | + ), |
| 138 | + ) |
| 139 | + .use( |
| 140 | + provider<VerificationCodeStorageService>( |
| 141 | + (_) => DependencyContainer.instance.verificationCodeStorageService, |
| 142 | + ), |
| 143 | + ) |
| 144 | + .use(provider<AuthService>((_) => DependencyContainer.instance.authService)) |
| 145 | + .use( |
| 146 | + provider<DashboardSummaryService>( |
| 147 | + (_) => DependencyContainer.instance.dashboardSummaryService, |
| 148 | + ), |
| 149 | + ) |
| 150 | + .use( |
| 151 | + provider<PermissionService>( |
| 152 | + (_) => DependencyContainer.instance.permissionService, |
| 153 | + ), |
| 154 | + ) |
| 155 | + .use( |
| 156 | + provider<UserPreferenceLimitService>( |
| 157 | + (_) => DependencyContainer.instance.userPreferenceLimitService, |
| 158 | + ), |
71 | 159 | );
|
72 | 160 | }
|
0 commit comments