checkAll method

Future<bool> checkAll(
  1. List<WebRoute> routing, {
  2. String parentPath = '',
})

Checks all routes to determine if a request matches any defined route.

The routing parameter is a list of WebRoute objects to check. The parentPath parameter is used for constructing nested routes.

Returns a Future<bool> indicating whether a matching route was found. If no match is found, it attempts to serve static files from the public directory or returns a 404 error if the file is not found. If an error occurs during file access, a 502 error is returned.

Implementation

Future<bool> checkAll(
  List<WebRoute> routing, {
  String parentPath = '',
}) async {
  for (var i = 0; i < routing.length; i++) {
    var route = routing[i];
    if (route.hosts.isNotEmpty && !route.hosts.contains('*')) {
      if (!route.hosts.contains(rq.host)) {
        continue;
      }
    }

    if (route.ports.isNotEmpty) {
      if (!route.ports.contains(rq.port)) {
        continue;
      }
    }

    List<String> paths = [route.path, ...route.extraPath];
    for (var path in paths) {
      path = joinPaths([parentPath, path]);
      route.setPathRender(path);
      rq.route = route;
      var res = await checkOne(route, path);
      if (res.found) {
        return true;
      }
    }
  }

  if (parentPath.isNotEmpty) return false;

  /// this lines works when nginx is not in local
  /// then it helps to load other files from public
  /// try to use nginx in production for better performance
  /// and security
  var publicFile = getFileFromPublic(rq.uri.path);
  try {
    if (publicFile.existsSync()) {
      renderFile(publicFile);
      return true;
    }
    rq.renderError(404, toData: rq.isApiEndpoint);
  } catch (e) {
    rq.renderError(502, toData: rq.isApiEndpoint);
  }

  return false;
}