staticHandler method

Future<Handler> staticHandler()

Implementation

Future<Handler> staticHandler() async {
  if (SimpodPaths.frontendIndexFile().existsSync()) {
    return createStaticHandler(
      SimpodPaths.flutterWebBuildPath,
      defaultDocument: 'index.html',
    );
  }

  if (Directory(SimpodPaths.simpodClientPath).existsSync()) {
    final process = await Process.start('flutter', [
      'build',
      'web',
      '--wasm',
    ], workingDirectory: SimpodPaths.simpodClientPath);

    print("Building flutter web application...");
    final stdoutFuture = process.stdout.drain<void>();
    final stderrFuture = process.stderr.pipe(stderr);

    final exitCode = await process.exitCode;
    await stdoutFuture;
    await stderrFuture;
    if (exitCode != 0) {
      throw Exception('flutter build failed');
    }

    print("\x1B[32m✓ Build completed\x1B[0m");
    print(
      '\x1B[90mServing web application from ${SimpodPaths.flutterWebBuildPath}\x1B[0m',
    );
    return createStaticHandler(
      SimpodPaths.flutterWebBuildPath,
      defaultDocument: 'index.html',
    );
  }

  stderr.writeln(
    'Warning: web dashboard assets not found; serving the API only.',
  );
  return (Request request) => Response.ok(
    '<!doctype html><html><body style="font-family: sans-serif">'
    '<h3>Simpod</h3>'
    '<p>The web dashboard assets are missing from this installation. '
    'The HTTP/WebSocket API is still available.</p>'
    '</body></html>',
    headers: {'Content-Type': 'text/html'},
  );
}