Skip to content

Commit 03eca2d

Browse files
committed
feat(core): initialize and provide application services in server
This commit updates `server.dart` to instantiate all application services, such as AuthService and DashboardSummaryService, injecting the necessary repository dependencies. These services are then provided to the application via the Dart Frog provider middleware, completing the centralized dependency injection setup.
1 parent 6dcbc8a commit 03eca2d

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

lib/src/config/server.dart

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
import 'package:dart_frog/dart_frog.dart';
44
import 'package:ht_api/src/config/environment_config.dart';
5+
import 'package:ht_api/src/rbac/permission_service.dart';
6+
import 'package:ht_api/src/services/auth_service.dart';
7+
import 'package:ht_api/src/services/auth_token_service.dart';
8+
import 'package:ht_api/src/services/dashboard_summary_service.dart';
9+
import 'package:ht_api/src/services/default_user_preference_limit_service.dart';
10+
import 'package:ht_api/src/services/jwt_auth_token_service.dart';
11+
import 'package:ht_api/src/services/token_blacklist_service.dart';
12+
import 'package:ht_api/src/services/user_preference_limit_service.dart';
13+
import 'package:ht_api/src/services/verification_code_storage_service.dart';
514
import 'package:ht_data_client/ht_data_client.dart';
615
import 'package:ht_data_postgres/ht_data_postgres.dart';
716
import 'package:ht_data_repository/ht_data_repository.dart';
17+
import 'package:ht_email_inmemory/ht_email_inmemory.dart';
18+
import 'package:ht_email_repository/ht_email_repository.dart';
819
import 'package:ht_shared/ht_shared.dart';
920
import 'package:logging/logging.dart';
1021
import 'package:postgres/postgres.dart';
@@ -124,9 +135,43 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
124135
toJson: (c) => c.toJson(),
125136
);
126137

127-
// 4. Create the main handler with all dependencies provided
138+
// 4. Initialize Services
139+
const uuid = Uuid();
140+
const emailRepository = HtEmailRepository(
141+
emailClient: HtEmailInMemoryClient(),
142+
);
143+
final tokenBlacklistService = InMemoryTokenBlacklistService();
144+
final authTokenService = JwtAuthTokenService(
145+
userRepository: userRepository,
146+
blacklistService: tokenBlacklistService,
147+
uuidGenerator: uuid,
148+
);
149+
final verificationCodeStorageService =
150+
InMemoryVerificationCodeStorageService();
151+
final authService = AuthService(
152+
userRepository: userRepository,
153+
authTokenService: authTokenService,
154+
verificationCodeStorageService: verificationCodeStorageService,
155+
emailRepository: emailRepository,
156+
userAppSettingsRepository: userAppSettingsRepository,
157+
userContentPreferencesRepository: userContentPreferencesRepository,
158+
uuidGenerator: uuid,
159+
);
160+
final dashboardSummaryService = DashboardSummaryService(
161+
headlineRepository: headlineRepository,
162+
categoryRepository: categoryRepository,
163+
sourceRepository: sourceRepository,
164+
);
165+
const permissionService = PermissionService();
166+
final userPreferenceLimitService = DefaultUserPreferenceLimitService(
167+
appConfigRepository: appConfigRepository,
168+
);
169+
170+
// 5. Create the main handler with all dependencies provided
128171
final finalHandler = handler
129-
.use(provider<Uuid>((_) => const Uuid()))
172+
// Foundational utilities
173+
.use(provider<Uuid>((_) => uuid))
174+
// Repositories
130175
.use(provider<HtDataRepository<Headline>>((_) => headlineRepository))
131176
.use(provider<HtDataRepository<Category>>((_) => categoryRepository))
132177
.use(provider<HtDataRepository<Source>>((_) => sourceRepository))
@@ -142,17 +187,34 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
142187
(_) => userContentPreferencesRepository,
143188
),
144189
)
145-
.use(provider<HtDataRepository<AppConfig>>((_) => appConfigRepository));
190+
.use(provider<HtDataRepository<AppConfig>>((_) => appConfigRepository))
191+
.use(provider<HtEmailRepository>((_) => emailRepository))
192+
// Services
193+
.use(provider<TokenBlacklistService>((_) => tokenBlacklistService))
194+
.use(provider<AuthTokenService>((_) => authTokenService))
195+
.use(
196+
provider<VerificationCodeStorageService>(
197+
(_) => verificationCodeStorageService,
198+
),
199+
)
200+
.use(provider<AuthService>((_) => authService))
201+
.use(provider<DashboardSummaryService>((_) => dashboardSummaryService))
202+
.use(provider<PermissionService>((_) => permissionService))
203+
.use(
204+
provider<UserPreferenceLimitService>(
205+
(_) => userPreferenceLimitService,
206+
),
207+
);
146208

147-
// 5. Start the server
209+
// 6. Start the server
148210
final server = await serve(
149211
finalHandler,
150212
ip,
151213
port,
152214
);
153215
_log.info('Server listening on port ${server.port}');
154216

155-
// 6. Handle graceful shutdown
217+
// 7. Handle graceful shutdown
156218
ProcessSignal.sigint.watch().listen((_) async {
157219
_log.info('Received SIGINT. Shutting down...');
158220
await _connection.close();

0 commit comments

Comments
 (0)