route method

Future<bool> route(
  1. Method method
)

Finding an Appropriate Method with Authorization Check

Implementation

Future<bool> route(Method method) async {
  // get mod path request
  final uriPath = uri.path
      .split('/')
      .map(
        (e) => e.replaceFirst(
          // 2b03f6be-c963-4493-8203-7928e195cc12.ext || 2b03f6be-c963-4493-8203-7928e195cc12 || 12
          RegExp(r'([a-z\d]+\-[a-z\d]+\-[a-z\d]+\-[a-z\d]+\-[a-z\d]+\.[a-zA-Z]+)|([a-z\d]+\-[a-z\d]+\-[a-z\d]+\-[a-z\d]+\-[a-z\d]+)|(\d+)'),
          '___',
        ),
      )
      .join('/');

  // get mod path api
  final apiPath = method.path.split('/').map((e) => e.replaceFirst(RegExp(r'\{[a-z]+\}'), '___')).join('/');

  if (this.method == method.method.name.toUpperCase() && uriPath == apiPath) {
    // check access
    if (!method.role.contains(UserRole.guest)) {
      await _checkMethodAuth(method);
    }
    // run method
    await method.func(this);
    return true;
  } else {
    return false;
  }
}