@@ -23,16 +23,17 @@ import 'package:logging/logging.dart';
23
23
import 'package:postgres/postgres.dart' ;
24
24
import 'package:uuid/uuid.dart' ;
25
25
26
- /// Global logger instance.
26
+ // This is the generated file from Dart Frog.
27
+ // We need to import it to get the `buildRootHandler` function.
28
+ import '../.dart_frog/server.dart' ;
29
+
30
+ // Global logger instance.
27
31
final _log = Logger ('ht_api' );
28
32
29
- /// Global PostgreSQL connection instance.
33
+ // Global PostgreSQL connection instance.
30
34
late final Connection _connection;
31
35
32
- /// Creates a data repository for a given type [T] .
33
- ///
34
- /// This helper function centralizes the creation of repositories,
35
- /// ensuring they all use the same database connection and logger.
36
+ // Creates a data repository for a given type [T].
36
37
HtDataRepository <T > _createRepository <T >({
37
38
required String tableName,
38
39
required FromJson <T > fromJson,
@@ -49,14 +50,7 @@ HtDataRepository<T> _createRepository<T>({
49
50
);
50
51
}
51
52
52
- /// The main entry point for the server.
53
- ///
54
- /// This function is responsible for:
55
- /// 1. Setting up the global logger.
56
- /// 2. Establishing the PostgreSQL database connection.
57
- /// 3. Providing these dependencies to the Dart Frog handler.
58
- /// 4. Gracefully closing the database connection on server shutdown.
59
- Future <HttpServer > run (Handler handler, InternetAddress ip, int port) async {
53
+ Future <void > main () async {
60
54
// 1. Setup Logger
61
55
Logger .root.level = Level .ALL ;
62
56
Logger .root.onRecord.listen ((record) {
@@ -88,17 +82,11 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
88
82
username: username,
89
83
password: password,
90
84
),
91
- // Using `require` is a more secure default. For local development against
92
- // a non-SSL database, this may need to be changed to `SslMode.disable`.
93
85
settings: const ConnectionSettings (sslMode: SslMode .require),
94
86
);
95
87
_log.info ('PostgreSQL database connection established.' );
96
88
97
89
// 3. Initialize and run database seeding
98
- // This runs on every startup. The operations are idempotent (`IF NOT EXISTS`,
99
- // `ON CONFLICT DO NOTHING`), so it's safe to run every time. This ensures
100
- // the database is always in a valid state, especially for first-time setup
101
- // in any environment.
102
90
final seedingService = DatabaseSeedingService (
103
91
connection: _connection,
104
92
log: _log,
@@ -181,11 +169,8 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
181
169
appConfigRepository: appConfigRepository,
182
170
);
183
171
184
- // 6. Populate the DependencyContainer with all initialized instances.
185
- // This must be done before the server starts handling requests, as the
186
- // root middleware will read from this container to provide dependencies.
172
+ // 6. Populate the DependencyContainer
187
173
DependencyContainer .instance.init (
188
- // Repositories
189
174
headlineRepository: headlineRepository,
190
175
categoryRepository: categoryRepository,
191
176
sourceRepository: sourceRepository,
@@ -195,7 +180,6 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
195
180
userContentPreferencesRepository: userContentPreferencesRepository,
196
181
appConfigRepository: appConfigRepository,
197
182
emailRepository: emailRepository,
198
- // Services
199
183
tokenBlacklistService: tokenBlacklistService,
200
184
authTokenService: authTokenService,
201
185
verificationCodeStorageService: verificationCodeStorageService,
@@ -205,10 +189,10 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
205
189
userPreferenceLimitService: userPreferenceLimitService,
206
190
);
207
191
208
- // 7. Start the server.
209
- // The original `handler` from Dart Frog is used. The root middleware in
210
- // `routes/_middleware.dart` will now be responsible for injecting all the
211
- // dependencies from the `DependencyContainer` into the request context.
192
+ // 7. Build the handler and start the server
193
+ final ip = InternetAddress .anyIPv4;
194
+ final port = int . parse ( Platform .environment[ 'PORT' ] ?? '8080' );
195
+ final handler = buildRootHandler ();
212
196
final server = await serve (handler, ip, port);
213
197
_log.info ('Server listening on port ${server .port }' );
214
198
@@ -221,6 +205,4 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
221
205
_log.info ('Server shut down.' );
222
206
exit (0 );
223
207
});
224
-
225
- return server;
226
208
}
0 commit comments