Skip to content

Commit 056fde1

Browse files
committed
refactor(server): remove unused reset logic from AppDependencies
Removes the `reset()` static method and makes the `_instance` field `final` again. This logic was introduced to support hot reloading, which has since been removed from the custom entrypoint. This change cleans up the `AppDependencies` class, removing dead code and preventing confusion about the server's startup behavior.
1 parent 7014237 commit 056fde1

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22

33
## Upcoming Release
4-
- **refactor!**: shift to eager loading for a robust, fail-fast startup that is fully compatible with Dart Frog's hot reload.
4+
- **refactor!**: shift to eager loading for a robust, fail-fast startup.
55

66
## 1.0.1 - 2025-10-17
77

bin/main.dart

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
import 'dart:io';
44
import 'package:dart_frog/dart_frog.dart';
5-
import 'package:flutter_news_app_api_server_full_source_code/src/config/app_dependencies.dart';
65
import '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`.
1210
import '../.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.
2422
Future<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

Comments
 (0)