22
33import 'dart:io' ;
44import 'package:dart_frog/dart_frog.dart' ;
5- import 'package:flutter_news_app_api_server_full_source_code/src/config/app_dependencies.dart' ;
65import 'package:logging/logging.dart' ;
7- import 'package:shelf_hotreload/shelf_hotreload.dart' ;
86
9- // Import the generated server entrypoint from the .dart_frog directory.
10- // This file contains the `createServer` function we need.
11- // We use a prefix to avoid a name collision with our own `main` function .
7+ import 'package:flutter_news_app_api_server_full_source_code/src/config/app_dependencies.dart' ;
8+
9+ // Import the generated server entrypoint to access `buildRootHandler` .
1210import '../.dart_frog/server.dart' as server;
1311
1412/// The main entrypoint for the application.
@@ -20,7 +18,7 @@ import '../.dart_frog/server.dart' as server;
2018/// If any part of the dependency initialization fails (e.g., database
2119/// connection, migrations), the process will log a fatal error and exit,
2220/// preventing the server from running in a broken state. This is a robust,
23- /// "fail-fast" approach that is compatible with Dart Frog's hot reload .
21+ /// "fail-fast" approach.
2422Future <void > main (List <String > args) async {
2523 // Use a local logger for startup-specific messages.
2624 // This is also the ideal place to configure the root logger for the entire
@@ -42,30 +40,22 @@ Future<void> main(List<String> args) async {
4240
4341 final log = Logger ('EagerEntrypoint' );
4442
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...' );
43+ try {
44+ log.info ('EAGER_INIT: Initializing application dependencies...' );
5345
54- // Eagerly initialize all dependencies. If this fails, it will throw.
55- await AppDependencies .instance.init ();
46+ // Eagerly initialize all dependencies. If this fails, it will throw.
47+ await AppDependencies .instance.init ();
5648
57- log.info ('EAGER_INIT: Dependencies initialized successfully.' );
58- log.info ('EAGER_INIT: Starting Dart Frog server...' );
49+ log.info ('EAGER_INIT: Dependencies initialized successfully.' );
50+ log.info ('EAGER_INIT: Starting Dart Frog server...' );
5951
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- );
52+ // Start the server directly without the hot reload wrapper.
53+ final address = InternetAddress .anyIPv6;
54+ final port = int .tryParse (Platform .environment['PORT' ] ?? '8080' ) ?? 8080 ;
55+ await serve (server.buildRootHandler (), address, port);
56+ } catch (e, s) {
57+ log.severe ('EAGER_INIT: FATAL: Failed to start server.' , e, s);
58+ // Exit the process if initialization fails.
59+ exit (1 );
60+ }
7161}
0 commit comments