public function process(ServerRequestInterface $request): ResponseInterface
    {
        $filteredMiddleware = $this->middleware;

        try {
            $request = $this->router->dispatch($request);

            $route = $request->getAttribute('route');
            $filteredMiddleware = array_filter($filteredMiddleware, function (Middleware $middleware) use ($route) {
                return $middleware->executeFor($route);
            });
            $requestHandler = $route->getHandler();
        } catch (HttpException $e) {
            $requestHandler = new NextHandler(function () use ($e) {
                throw $e;
            });
        }

        $filteredMiddleware = array_map(function (Middleware $middleware) {
            return $middleware->getMiddleware();
        }, $filteredMiddleware);

        $dispatcher = new Stack(
            $filteredMiddleware,
            $requestHandler
        );

        return $dispatcher->dispatch($request);
    }