boot method

Future<void> boot()

Boots the application by initializing core services and providers.

Sets up:

Then boots all registered providers and triggers onBooted callbacks.

Sets status to AppStatus.ready on success, or AppStatus.error on failure.

Implementation

Future<void> boot() async {
  status = AppStatus.booting;

  // Default Router
  final router = Router();
  container.bindInstance<Router>(router);

  // Default View Engine
  final settings = {"viewsPath": 'lib/src/http/views', "publicPath": 'lib/src/http/public'};
  final engine = TemplateEngine(viewsDirectory: settings['viewsPath']!, publicDirectory: settings['publicPath']!);

  // Enable caching for performance
  engine.shouldCache = true; // Enabled by default for performance recommendation
  // engine.shouldCache = config?.get('view.cache', true) ?? true;
  container.bindInstance<TemplateEngine>(engine);

  // Default Static Files Server
  final staticFilesServer = StaticFilesServer();
  container.bindInstance<StaticFilesServer>(staticFilesServer);

  // Default UUID Generator
  final Uuid uuid = Uuid();
  container.bindInstance<Uuid>(uuid);

  // Default SQLite DB
  final Directory dir = Directory("lib/src/storage");
  final file = File("${dir.absolute.path}/database.sqlite");
  final SQLiteDatabase sqliteDatabase = await databaseFactoryFfi.openDatabase(
    file.absolute.path,
    options: OpenDatabaseOptions(
      version: 1,
      onCreate: (db, version) async {
        // Placeholder for database migrations
      },
    ),
  );
  container.singleton<SQLiteDatabase>(factory: (_, [_]) => sqliteDatabase, eager: true);

  // Default Postgres DB
  try {
    final postgresConnection = await PostgresDatabase.open(
      Endpoint(host: config.get('db.pgsql.host', ''), database: config.get('db.pgsql.database', ''), username: config.get('db.pgsql.username', ''), password: config.get('db.pgsql.password', '')),
      // The postgres server hosted locally doesn't have SSL by default. If you're
      // accessing a postgres server over the Internet, the server should support
      // SSL and you should swap out the mode with `SslMode.verifyFull`.
      settings: ConnectionSettings(sslMode: SslMode.disable),
    );

    container.bindInstance<PostgresDatabase>(postgresConnection);

  } catch (e, s) {
    App().archeryLogger.error("Postgres DB Init Error", {"origin": "App.boot()", "error": e.toString(), "stack": config.get('app.debug') != null && config.get('app.debug') == true ? s.toString() : ''});
  }

  await container.initialize();

  try {
    // Boot all registered providers
    for (final provider in _providers) {
      try {
        await provider.boot(container);
      } catch (e, stack) {
        throw ProviderException.unbooted(type: provider.runtimeType, trace: stack);
      }
    }

    // Execute post-boot callbacks
    for (final callback in _bootedCallbacks) {
      callback();
    }

    status = AppStatus.ready;
  } catch (e, stack) {
    status = AppStatus.error;
    print('[App Boot Error] $e\n$stack');
    rethrow; // Optional: allow external handling
  }
}