Skip to content

Commit ae9a641

Browse files
committed
fix(api): provide all dependencies from root middleware
Refactors the root middleware to provide all shared repositories and services to the request context. It reads the initialized instances from the `DependencyContainer` singleton, which is populated at startup. This change completes the architectural refactoring to a centralized dependency injection model, ensuring that all dependencies are available to downstream middleware and route handlers, finally resolving the "context.read() called too early" errors.
1 parent 2880ad6 commit ae9a641

File tree

1 file changed

+99
-11
lines changed

1 file changed

+99
-11
lines changed

routes/_middleware.dart

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import 'package:dart_frog/dart_frog.dart';
2+
import 'package:ht_api/src/config/dependency_container.dart';
23
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';
314
import 'package:uuid/uuid.dart';
415

516
// --- Request ID Wrapper ---
@@ -46,27 +57,104 @@ class RequestId {
4657

4758
// --- Middleware Definition ---
4859
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.
5564
return handler
65+
// --- Core Middleware ---
66+
// These run after all dependencies have been provided.
5667
.use(errorHandler())
5768
.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.
5872
.use((innerHandler) {
59-
// This middleware reads the Uuid and provides the RequestId.
60-
// It must come after the Uuid provider in the chain.
6173
return (context) {
62-
// Read the Uuid instance provided from the previous middleware.
6374
final uuid = context.read<Uuid>();
6475
final requestId = RequestId(uuid.v4());
6576
return innerHandler(context.provide<RequestId>(() => requestId));
6677
};
6778
})
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()))
6884
.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+
),
71159
);
72160
}

0 commit comments

Comments
 (0)