我正在寻找一种方法,在 Cowboy 中,将任意路径(存储在数据库中)映射到特定的博客文章。
那就是:我有几千篇博客文章,每个都可以通过几个名称访问,例如规范 URL(例如/post/42
)、一些别名(例如/2013/11/25/erlang-rocks
)、历史位置(例如/path-on-old-blog/12345
)等。
我知道我可以简单地使用一条包罗万象的路线:
{ "/[...]", catch_all_handler, [] },
...然后在数据库中查找路径,但我正在考虑从数据库创建路由,如下所示:
Posts = posts:all(),
Paths = [get_handlers_for_post(P) || P <- Posts],
Routes = lists:flatten(Paths),
get_handler_for_post(P) ->
% Generate a list of paths with IDs from the database.
% Return something that looks like this:
% [{"/canonical/1", post_handler, [1]},
% {"/first-alias", post_handler, [1]}].
% TODO: code goes here...
即:将所有可能的路径放入路由器中,指向同一个处理程序,每条路径都有帖子的ID。
问题是:这合理吗?牛仔支持多少条路线?