handleRequest method

Future<bool> handleRequest(
  1. RequestContext req,
  2. ResponseContext res
)

Attempts to handle a request, according to its hostname.

If none is matched, then true is returned. Also returns true if all of the sub-app's handlers returned true.

Implementation

Future<bool> handleRequest(RequestContext req, ResponseContext res) async {
  for (var pattern in _patterns) {
    // print('${req.hostname} vs $_creators');
    if (pattern.allMatches(req.hostname).isNotEmpty) {
      // Resolve the entire pipeline within the context of the selected app.
      var app = _apps[pattern] ??= (await _creators[pattern]!());
      // print('App for ${req.hostname} = $app from $pattern');
      // app.dumpTree();

      var r = app.optimizedRouter;
      var resolved = r.resolveAbsolute(req.path, method: req.method);
      var pipeline = MiddlewarePipeline<RequestHandler>(resolved);
      // print('Pipeline: $pipeline');
      for (var handler in pipeline.handlers) {
        // print(handler);
        // Avoid stack overflow.
        if (handler == handleRequest) {
          continue;
        } else if (!await app.executeHandler(handler, req, res)) {
          // print('$handler TERMINATED');
          return false;
        } else {
          // print('$handler CONTINUED');
        }
      }
    }
  }

  // Otherwise, return true.
  return true;
}