handler method

  1. @override
FutureOr<ResponseEntity> handler(
  1. RequestEntity request
)
override

Implementation

@override
FutureOr<ResponseEntity> handler(RequestEntity request) {
  ///find routes that match with the path
  String urlPath = '/${request.url.path}';
  List<Route> matchedRoutes = routes
      .where(
        (element) => element.match(urlPath),
      )
      .toList();

  ///no routes found: 404
  if (matchedRoutes.isEmpty) {
    return ResponseEntity.notFound();
  } else {
    ///there is some route, check method (get, post, put...)
    Route? finalRoute = matchedRoutes.firstWhereOrNull(
      (element) => element.method == HttpMethod(request.method),
    );
    if (finalRoute == null) {
      ///no route matching method: 415
      return ResponseEntity.methodNotAllowed();
    } else {
      return finalRoute.handler(request);
    }
  }
}