resolveAndExecuteHandlers method

Future<ReqRes> resolveAndExecuteHandlers(
  1. Request req,
  2. Response res
)

Implementation

Future<ReqRes> resolveAndExecuteHandlers(Request req, Response res) async {
  ReqRes reqRes = (req: req, res: res);

  @pragma('vm:prefer-inline')
  Response routeNotFound() => res.notFound("Route not found: ${req.path}");

  final routeResult = spanner.lookup(req.method, req.uri);
  final resolvedHandlers = routeResult?.values ?? const [];
  if (routeResult == null || resolvedHandlers.isEmpty) {
    return reqRes.merge(routeNotFound());
  }

  if (routeResult.params.isNotEmpty) {
    req.params.addAll(routeResult.params);
  }

  reqRes = await executeHandlers(resolvedHandlers, reqRes);

  for (final job in _preResponseHooks) {
    reqRes = await Future.microtask(() => job(reqRes));
  }

  if (!reqRes.res.ended) {
    return reqRes.merge(routeNotFound());
  }

  return reqRes;
}