Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
fulleni wants to merge 16 commits into main from feature/eager-init
Closed
Show file tree
Hide file tree
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 Oct 19, 2025
b45e889
refactor(server): simplify AppDependencies for eager loading
fulleni Oct 19, 2025
f5b81e3
refactor(server): remove lazy init from root middleware
fulleni Oct 19, 2025
895be8a
refactor(server): configure custom executable entrypoint
fulleni Oct 19, 2025
048eaa3
docs(readme): add eager loading architecture highlight
fulleni Oct 19, 2025
fb0795c
docs(changelog): document eager loading refactor
fulleni Oct 19, 2025
22d92ea
refactor(server): move logger setup to eager entrypoint
fulleni Oct 19, 2025
7765ee0
fix(server): correct entrypoint logic and linter warnings
fulleni Oct 19, 2025
0864728
feat(bin): implement hot reload for Dart Frog server
fulleni Oct 19, 2025
7014237
refactor(loading): update eager loading description for Dart Frog com...
fulleni Oct 19, 2025
056fde1
refactor(server): remove unused reset logic from AppDependencies
fulleni Oct 19, 2025
758765d
style: format
fulleni Oct 19, 2025
a46581e
feat(server): implement graceful shutdown and atomic logging
fulleni Oct 19, 2025
38a3a5d
fix(bin): handle SIGTERM signal support on non-Windows platforms
fulleni Oct 19, 2025
a588d98
fix(server): resolve handler type ambiguity in entrypoint
fulleni Oct 19, 2025
301c0e4
fix(server): ensure fatal startup logs are captured before exit
fulleni Oct 19, 2025
File filter

Filter by extension

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.

3 changes: 3 additions & 0 deletions CHANGELOG.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Upcoming Release
- **refactor!**: shift to eager loading for a robust, fail-fast startup.

## 1.0.1 - 2025年10月17日

- **chore**: A new migration ensures that existing user preference documents are updated to include the savedFilters field, initialized as an empty array.
Expand Down
7 changes: 7 additions & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ Click on any category to explore.

---

### ✨ Eager Loading & Fail-Fast Startup
- **Robust Initialization:** The server now initializes all critical dependencies (database connection, migrations, seeding) *before* it starts accepting any requests.
- **Fail-Fast by Design:** If any part of the startup process fails (e.g., the database is down), the server will immediately exit with a clear error.
> **Your Advantage:** This eliminates startup race conditions and ensures the server is either fully operational or not running at all. It provides a highly predictable and stable production environment, preventing the server from running in a broken state.

---

### 🔌 Robust Dependency Injection
- **Testable & Modular:** A centralized dependency injection system makes the entire application highly modular and easy to test.
- **Swappable Implementations:** Easily swap out core components—like the database (MongoDB), email provider (SendGrid), or storage services—without rewriting your business logic.
Expand Down
92 changes: 92 additions & 0 deletions bin/main.dart
View file Open in desktop
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}');
}

// 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.
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);
}
}
Loading
Loading

AltStyle によって変換されたページ (->オリジナル) /