run method

FutureOr<Response> run(
  1. Request request,
  2. String context
)

Run the execution for given request

Implementation

FutureOr<Response> run(Request request, String context) async {
  setResource('context', () => context, context: context);
  setResource('request', () => request, context: context);

  try {
    getResource('response', context: context);
  } catch (e) {
    setResource('response', () => Response(''), context: context);
  }

  var method = request.method.toUpperCase();
  var route = match(request);
  final groups = (route is Route) ? route.getGroups() : <String>[];

  if (method == Request.head) {
    method = Request.get;
  }

  if (route == null && _wildcardRoute != null) {
    route = _wildcardRoute;
    route!.path = request.url.path;
  }

  if (route != null) {
    return execute(route, request, context);
  } else if (method == Request.options) {
    try {
      _executeHooks(
        _options,
        groups,
        (hook) async => _getArguments(
          hook,
          context: context,
          requestParams: await request.getParams(),
        ),
        globalHook: true,
        globalHooksFirst: false,
      );
      return getResource<Response>('response', context: context);
    } on Exception catch (e) {
      for (final hook in _errors) {
        _di.set('error', () => e);
        if (hook.getGroups().contains('*')) {
          hook.getAction().call(
                _getArguments(
                  hook,
                  context: context,
                  requestParams: await request.getParams(),
                ),
              );
        }
      }
      return getResource<Response>('response', context: context);
    }
  }
  final response = getResource<Response>('response', context: context);
  response.text('Not Found');
  response.status = 404;

  // for each run, resources should be re-generated from callbacks
  resetResources(context);

  return response;
}