【发布时间】:2014-01-19 17:27:54
【问题描述】:
我正在 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...
即:将所有可能的路径放入路由器中,指向同一个handler,每个都有post的ID。
问题是:这合理吗?牛仔支持多少条路线?
【问题讨论】:
-
Cowboy 路线非常有效,您可以为几乎任何东西创建模式,但我想 nginx 对于这种情况仍然会更好。如果你能举几个例子说明你的网址是什么样的,也许我们可以更好地帮助你:)