-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/eager init #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5fca19c
feat(server): implement eager-loading entrypoint
fulleni b45e889
refactor(server): simplify AppDependencies for eager loading
fulleni f5b81e3
refactor(server): remove lazy init from root middleware
fulleni 895be8a
refactor(server): configure custom executable entrypoint
fulleni 048eaa3
docs(readme): add eager loading architecture highlight
fulleni fb0795c
docs(changelog): document eager loading refactor
fulleni 22d92ea
refactor(server): move logger setup to eager entrypoint
fulleni 7765ee0
fix(server): correct entrypoint logic and linter warnings
fulleni 0864728
feat(bin): implement hot reload for Dart Frog server
fulleni 7014237
refactor(loading): update eager loading description for Dart Frog com...
fulleni 056fde1
refactor(server): remove unused reset logic from AppDependencies
fulleni 758765d
style: format
fulleni a46581e
feat(server): implement graceful shutdown and atomic logging
fulleni 38a3a5d
fix(bin): handle SIGTERM signal support on non-Windows platforms
fulleni a588d98
fix(server): resolve handler type ambiguity in entrypoint
fulleni 301c0e4
fix(server): ensure fatal startup logs are captured before exit
fulleni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
bin/main.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // ignore_for_file: avoid_print | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:dart_frog/dart_frog.dart'; | ||
| import 'package:flutter_news_app_api_server_full_source_code/src/config/app_dependencies.dart'; | ||
| import 'package:logging/logging.dart'; | ||
|
|
||
| // Import the generated server entrypoint to access `buildRootHandler`. | ||
| import '../.dart_frog/server.dart' as dart_frog; | ||
|
|
||
| /// The main entrypoint for the application. | ||
| /// | ||
| /// This custom entrypoint implements an "eager loading" strategy. It ensures | ||
| /// that all critical application dependencies are initialized *before* the | ||
| /// HTTP server starts listening for requests. | ||
| /// | ||
| /// If any part of the dependency initialization fails (e.g., database | ||
| /// connection, migrations), the process will log a fatal error and exit, | ||
| /// preventing the server from running in a broken state. This is a robust, | ||
| /// "fail-fast" approach. | ||
| Future<void> main(List<String> args) async { | ||
| // Use a local logger for startup-specific messages. | ||
| // This is also the ideal place to configure the root logger for the entire | ||
| // application, as it's guaranteed to run only once at startup. | ||
| Logger.root.level = Level.ALL; | ||
| Logger.root.onRecord.listen((record) { | ||
| final message = StringBuffer() | ||
| ..write('${record.level.name}: ${record.time}: ${record.loggerName}: ') | ||
| ..writeln(record.message); | ||
|
|
||
| if (record.error != null) { | ||
| message.writeln(' ERROR: ${record.error}'); | ||
| } | ||
| if (record.stackTrace != null) { | ||
| message.writeln(' STACK TRACE: ${record.stackTrace}'); | ||
| } | ||
fulleni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Write the log message atomically to stdout. | ||
| stdout.write(message.toString()); | ||
| }); | ||
|
|
||
| final log = Logger('EagerEntrypoint'); | ||
| HttpServer? server; | ||
|
|
||
| Future<void> shutdown([String? signal]) async { | ||
| log.info('Received ${signal ?? 'signal'}. Shutting down gracefully...'); | ||
| // Stop accepting new connections. | ||
| await server?.close(); | ||
| // Dispose all application dependencies. | ||
| await AppDependencies.instance.dispose(); | ||
| log.info('Shutdown complete.'); | ||
| exit(0); | ||
| } | ||
|
|
||
| // Listen for termination signals. | ||
| ProcessSignal.sigint.watch().listen((_) => shutdown('SIGINT')); | ||
| // SIGTERM is not supported on Windows. Attempting to listen to it will throw. | ||
fulleni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!Platform.isWindows) { | ||
| ProcessSignal.sigterm.watch().listen((_) => shutdown('SIGTERM')); | ||
| } | ||
|
|
||
| try { | ||
| log.info('EAGER_INIT: Initializing application dependencies...'); | ||
|
|
||
| // Eagerly initialize all dependencies. If this fails, it will throw. | ||
| await AppDependencies.instance.init(); | ||
|
|
||
| log.info('EAGER_INIT: Dependencies initialized successfully.'); | ||
| log.info('EAGER_INIT: Starting Dart Frog server...'); | ||
|
|
||
| // Start the server directly without the hot reload wrapper. | ||
| final address = InternetAddress.anyIPv6; | ||
| final port = int.tryParse(Platform.environment['PORT'] ?? '8080') ?? 8080; | ||
|
|
||
| // Explicitly cast the handler to resolve the type ambiguity. | ||
| final handler = dart_frog.buildRootHandler() as Handler; | ||
| server = await serve(handler, address, port); | ||
| log.info( | ||
| 'Server listening on http://${server.address.host}:${server.port}', | ||
| ); | ||
| } catch (e, s) { | ||
| log.severe('EAGER_INIT: FATAL: Failed to start server.', e, s); | ||
| // Log directly to stderr and flush to ensure the message is captured | ||
| // before the process exits, which is crucial for debugging startup errors. | ||
| stderr.writeln('EAGER_INIT: FATAL: Failed to start server. Error: $e\nStack Trace: $s'); | ||
| await stderr.flush(); | ||
| // Exit the process if initialization fails. | ||
| exit(1); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.