如何在Yaws / Erlang中重写URL

时间:2016-07-29 20:25:01

标签: erlang yaws

如何在不包含延伸的情况下访问yaws文件?说,

www.domain.com/listen.yaws =>的 www.domain.com/listen

我在yaws documentation / appmod。

中找不到任何具体的文档

我认为问题最终得到澄清!

1 个答案:

答案 0 :(得分:5)

您可以在the Yaws PDF documentation的“Arg Rewrite”部分(7.1.2)中找到一个如何完成此操作的示例。将服务器配置中的变量arg_rewrite_mod设置为支持重写的Erlang模块的名称:

arg_rewrite_mod = my_rewriter

为支持重写,my_rewriter模块必须定义并导出arg_rewrite/1函数,并将#arg{}记录作为其参数:

-module(my_rewriter).
-export([arg_rewrite/1]).

-include_lib("yaws/include/yaws_api.hrl").

rewrite_pages() ->
    ["/listen"].

arg_rewrite(Arg) ->
    Req = Arg#arg.req,
    {abs_path, Path} = Req#http_request.path,
    case lists:member(Path, rewrite_pages()) of
        true ->
            Arg#arg{req = Req#http_request{path = {abs_path, Path++".yaws"}}};
        false ->
            Arg
    end.

代码包含yaws_api.hrl以获取#arg{}记录定义。

rewrite_pages/0函数返回必须重写的页面列表,以包含".yaws"个后缀;在此示例中,它只是您在问题中提到的/listen页面。如果在arg_rewrite/1中我们在该列表中找到了请求的页面,我们会将".yaws"附加到页面名称并将其包含在新的#arg{}中,然后返回到Yaws,然后Yaws继续根据请求发送请求在新的#arg{}上。