Skip to content

Commit a801d84

Browse files
committed
feat: Load and provide AppConfig via middleware
- Loads AppConfig asynchronously - Provides AppConfig via context
1 parent 64b132b commit a801d84

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

routes/_middleware.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'dart:async';
23
import 'dart:io';
34

45
import 'package:dart_frog/dart_frog.dart';
@@ -23,6 +24,9 @@ import 'package:ht_email_repository/ht_email_repository.dart';
2324
import 'package:ht_shared/ht_shared.dart';
2425
import 'package:uuid/uuid.dart';
2526

27+
// Assuming a fixed ID for the AppConfig document
28+
const String _appConfigId = 'app_config';
29+
2630
// --- Request ID Wrapper ---
2731

2832
/// {@template request_id}
@@ -247,6 +251,20 @@ HtDataRepository<SuggestedContentTemplate>
247251
return HtDataRepository<SuggestedContentTemplate>(dataClient: client);
248252
}
249253

254+
/// Middleware to asynchronously load and provide the AppConfig.
255+
Middleware _appConfigProviderMiddleware() {
256+
return (handler) {
257+
return (context) async {
258+
// Read the AppConfigRepository from the context
259+
final appConfigRepository = context.read<HtDataRepository<AppConfig>>();
260+
// Read the AppConfig instance
261+
final appConfig = await appConfigRepository.read(id: _appConfigId);
262+
// Provide the AppConfig instance to downstream handlers/middleware
263+
return handler(context.provide<AppConfig>(() => appConfig));
264+
};
265+
};
266+
}
267+
250268
// --- Middleware Definition ---
251269
Handler middleware(Handler handler) {
252270
// Initialize repositories when middleware is first created
@@ -341,6 +359,8 @@ Handler middleware(Handler handler) {
341359
// (or early in the "response" phase) to catch errors from upstream.
342360
// ==========================================================================
343361
return handler
362+
// Add the asynchronous AppConfig provider middleware here
363+
.use(_appConfigProviderMiddleware())
344364
// --- 1. Request ID Provider (Early Setup) ---
345365
// PURPOSE: Generates a unique ID (UUID v4) for each incoming request.
346366
// Provides `RequestId` instance via context.
@@ -410,6 +430,7 @@ Handler middleware(Handler handler) {
410430
),
411431
)
412432

433+
413434
// --- 4. Authentication Service Providers (Auth Logic Dependencies) ---
414435
// PURPOSE: Provide the core services needed for authentication logic.
415436
// ORDER: These MUST be provided BEFORE `authenticationProvider` and

0 commit comments

Comments
 (0)