问题描述
我正在尝试在Mojolicious应用程序中整理一个维护页面,只要服务器上存在文件或数据库条目,我的所有用户都将被显示.
I am trying to put together a maintenance page in my Mojolicious app which all of my users will be shown whenever a file or DB entry is present on the server.
我知道我可以在启动时检查此文件或条目,并且是否在其中添加了全部捕获"路由.但是我不确定如何动态地执行此操作?我不想在需要维护时就必须重新启动后端.
I know I can check for this file or entry on startup and if its there add in my 'catch all' route. However I'm not sure how to do this dynamically? I don't want to have to restart the backend whenever I want to go into maintenance.
有没有一种方法可以从钩子中添加和删除路由?例如,使用before dispatch钩子来监视文件/数据库条目,如果存在,则修改路由?
Is there a way to add and remove routes from a hook? for example use the before dispatch hook to monitor for the file/db entry and if it exists modify the routes?
我尝试了此操作,但似乎无法从挂钩函数访问路由,只能在启动函数中访问
I tried this but I didn't seem to be able to access the routes from the hooked function, only in the startup function.
谢谢.
推荐答案
路由器是动态的,直到满足第一个请求后,路由器才能更改路由().就是说,您不能一般地声明路由,仅在存在该条件之前禁止任何访问吗?
The router is dynamic until the first request has been served, after that, the router cannot change routes (source). That said, can you not declare the route generally and just prohibit any access until that condition exists?
#!/usr/bin/env perl
use Mojolicious::Lite;
any '/' => sub { shift->render( text => 'Hello World' ) };
under sub {
unless (-e 'myfile') {
shift->render_not_found;
return 0;
}
return 1;
};
any '/protected' => sub { shift->render( text => 'I am safe' ) };
app->start;
这篇关于如何在Mojolicious中动态添加和删除路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!