如何在 Cro 中插入一些路由检查中间件?

jjm*_*elo 5 microservices cro raku

假设我需要在提供一些结果之前检查一些 URI。我可以做这样的事情:

sub type-routes {
    route {
        get -> Str $type where $type ? @food-types {
            my %ingredients-table = $rrr.calories-table;
            my @result =  %ingredients-table.keys.grep: {
                %ingredients-table{$_}{$type} };
            content 'application/json', @result;
        }
        get -> Str $type where $type ? @food-types {
            not-found;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,在这种情况下,现有产品和非现有产品的签名不同。但是,将在所有路由中使用相同的 URI。能够任何路由匹配之前检查它会很有趣,这样当它到达路由块时,我们就知道它没问题。这样它也可以在不同的路线上重复使用。

我已经检查了beforeandbefore-match,显然你可以做到,但是你需要分析请求对象才能做到这一点,没有简单的方法可以做到。

或者,是否有任何定义“回退路由”的方法,以便如果未找到 URI,则返回 not-found ?

Jon*_*ton 11

在给出中间件答案之前,我在这种情况下的第一选择通常是一种subset类型:

sub type-routes {
    route {
        my constant @food-types = <burgers pizzas beer>;
        my subset Food of Str where { $^type ? @food-types }

        get -> Food $type {
            my %ingredients-table = $rrr.calories-table;
            my @result =  %ingredients-table.keys.grep: {
                %ingredients-table{$_}{$type} };
            content 'application/json', @result;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,Food可以根据需要在任意多的路线上使用。不需要回退路由(无论是在这个解决方案中还是在原始问题中)not-found,因为当没有路由匹配路径段时,路由器会自动生成。

但是,如果想要采用中间件方式,那么最简单的方法是获取path-segmentsrequest并检查适当的部分:

sub type-routes {
    route {
        my constant @food-types = <burgers pizzas beer>;
        before {
            not-found unless request.path-segments[0] ? @food-types;
        }

        get -> Str $type {
            content 'application/json', <123 456>;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)