11// ignore_for_file: avoid_print
22
33import 'dart:io' ;
4-
4+ import 'package:dart_frog/dart_frog.dart' ;
55import 'package:flutter_news_app_api_server_full_source_code/src/config/app_dependencies.dart' ;
66import 'package:logging/logging.dart' ;
7+ import 'package:shelf_hotreload/shelf_hotreload.dart' ;
78
89// Import the generated server entrypoint from the .dart_frog directory.
10+ // This file contains the `createServer` function we need.
911// We use a prefix to avoid a name collision with our own `main` function.
1012import '../.dart_frog/server.dart' as server;
1113
@@ -18,7 +20,7 @@ import '../.dart_frog/server.dart' as server;
1820/// If any part of the dependency initialization fails (e.g., database
1921/// connection, migrations), the process will log a fatal error and exit,
2022/// preventing the server from running in a broken state. This is a robust,
21- /// "fail-fast" approach suitable for production environments .
23+ /// "fail-fast" approach that is compatible with Dart Frog's hot reload .
2224Future <void > main (List <String > args) async {
2325 // Use a local logger for startup-specific messages.
2426 // This is also the ideal place to configure the root logger for the entire
@@ -40,21 +42,30 @@ Future<void> main(List<String> args) async {
4042
4143 final log = Logger ('EagerEntrypoint' );
4244
43- try {
44- log.info ('EAGER_INIT: Initializing application dependencies...' );
45+ // This is our custom hot-reload-aware startup logic.
46+ // The `withHotreload` function from `shelf_hotreload` (used by Dart Frog)
47+ // takes a builder function that it calls whenever a reload is needed.
48+ // We place our initialization logic inside this builder.
49+ withHotreload (
50+ () async {
51+ try {
52+ log.info ('EAGER_INIT: Initializing application dependencies...' );
4553
46- // Eagerly initialize all dependencies. If this fails, it will throw.
47- await AppDependencies .instance.init ();
54+ // Eagerly initialize all dependencies. If this fails, it will throw.
55+ await AppDependencies .instance.init ();
4856
49- log.info ('EAGER_INIT: Dependencies initialized successfully.' );
50- log.info ('EAGER_INIT: Starting Dart Frog server...' );
57+ log.info ('EAGER_INIT: Dependencies initialized successfully.' );
58+ log.info ('EAGER_INIT: Starting Dart Frog server...' );
5159
52- // Only if initialization succeeds, start the Dart Frog server.
53- // This function is void and handles its own async logic internally,
54- // so it should be called without `await`.
55- server.main ();
56- } catch (e, s) {
57- log.severe ('EAGER_INIT: FATAL: Failed to start server.' , e, s);
58- exit (1 ); // Exit with a non-zero code to indicate failure.
59- }
60+ // Use the generated `createServer` function from Dart Frog.
61+ final address = InternetAddress .anyIPv6;
62+ const port = 8080 ;
63+ return serve (server.buildRootHandler (), address, port);
64+ } catch (e, s) {
65+ log.severe ('EAGER_INIT: FATAL: Failed to start server.' , e, s);
66+ // Exit the process if initialization fails.
67+ exit (1 );
68+ }
69+ },
70+ );
6071}
0 commit comments