Future run()

in app/lib/fake/server/fake_server_entrypoint.dart [58:164]


  Future<void> run() async {
    final port = int.parse(argResults!['port'] as String);
    final storagePort = int.parse(argResults!['storage-port'] as String);
    final searchPort = int.parse(argResults!['search-port'] as String);
    final analyzerPort = int.parse(argResults!['analyzer-port'] as String);
    final dartdocPort = int.parse(argResults!['dartdoc-port'] as String);
    final readOnly = argResults!['read-only'] == true;
    final dataFile = argResults!['data-file'] as String?;
    final watch = argResults!['watch'] == true;

    Logger.root.onRecord.listen((r) {
      print([
        r.time.toIso8601String(),
        r.toString(),
        r.error,
        r.stackTrace?.toString(),
      ].where((e) => e != null).join(' '));
    });

    final state = LocalServerState();
    if (dataFile != null) {
      await state.loadIfExists(dataFile);
    }

    final storage = state.storage;
    final datastore = state.datastore;

    final storageServer = FakeStorageServer(storage);
    final pubServer = FakePubServer(datastore, storage, watch: watch);
    final searchService = FakeSearchService(datastore, storage);
    final analyzerService = FakeAnalyzerService(datastore, storage);
    final dartdocService = FakeDartdocService(datastore, storage);

    final configuration = Configuration.fakePubServer(
      frontendPort: port,
      storageBaseUrl: 'http://localhost:$storagePort',
      searchPort: searchPort,
    );

    Future<shelf.Response> _updateUpstream(int port) async {
      final rs =
          await post(Uri.parse('http://localhost:$port/fake-update-all'));
      if (rs.statusCode == 200) {
        return shelf.Response.ok('OK');
      } else {
        return shelf.Response(503,
            body: 'Upstream service ($port) returned ${rs.statusCode}.');
      }
    }

    Future<shelf.Response> forwardUpdatesHandler(shelf.Request rq) async {
      if (rq.method.toUpperCase() == 'POST' &&
          rq.requestedUri.path == '/fake-test-profile') {
        return await _chainHandlers([
          () => _testProfile(rq),
          () => _updateUpstream(searchPort),
        ]);
      }
      if (rq.requestedUri.path == '/fake-update-all') {
        return await _chainHandlers([
          () => _updateUpstream(analyzerPort),
          () => _updateUpstream(dartdocPort),
          () => _updateUpstream(searchPort),
        ]);
      }
      if (rq.requestedUri.path == '/fake-update-analyzer') {
        return await _updateUpstream(analyzerPort);
      }
      if (rq.requestedUri.path == '/fake-update-dartdoc') {
        return await _updateUpstream(dartdocPort);
      }
      if (rq.requestedUri.path == '/fake-update-search') {
        return await _updateUpstream(searchPort);
      }
      return shelf.Response.notFound('Not Found.');
    }

    await updateLocalBuiltFilesIfNeeded();
    await Future.wait(
      [
        storageServer.run(port: storagePort),
        pubServer.run(
          port: port,
          configuration: configuration,
          extraHandler: forwardUpdatesHandler,
        ),
        searchService.run(
          port: searchPort,
          configuration: configuration,
        ),
        analyzerService.run(
          port: analyzerPort,
          configuration: configuration,
        ),
        dartdocService.run(
          port: dartdocPort,
          configuration: configuration,
        ),
      ],
      eagerError: true,
    );

    if (!readOnly && dataFile != null) {
      await state.save(dataFile);
    }
    print('Server processes completed.');
  }