2015-1-22【erlang】Cowboy学习记录-关于Routing章节

官方文档:http://ninenines.eu/docs/en/cowboy/HEAD/guide/routing/

Cowboy does nothing by default.

翻译:

(忽略。。。。)

To make Cowboy useful, you need to map URLs to Erlang modules that will handle the requests. This is called routing.

翻译:

未了使Cowboy可用,需要将(请求中的)URL与Erlang模块(ps:用来处理request的)映射起来。这就叫做“路由”。(下文还是叫routing好了)


                 图1 Routing示意


When Cowboy receives a request, it tries to match the requested host and path to the resources given in the dispatch rules. If it matches, then the associated Erlang code will be executed.

翻译:

当Cowboy收到一个请求的时候,回提取出RUL中的host和path部分(htttp://127.0.0.1/somepath/index.html       127.0.0.1 即为host,/somepath/index.html即为path吧),去跟dispatch 规则中的资源去匹配。如果匹配上了,则对应的Erlang代码将会被执行。

Routing rules are given per host. Cowboy will first match on the host, and then try to find a matching path.

翻译:

每个host都有一个routing规则。Cowboy会先在上面的dispatch规则中先找到匹配的host,然后再去找匹配的path。

Routes need to be compiled before they can be used by Cowboy.

翻译:

(忽略。。。。)


Structure

(就是层层解释下结构是如何的)

The general structure for the routes is defined as follow.

翻译:

(忽略。。。。)

1
Routes = [ Host1 , Host2 , ... HostN ].

Each host contains matching rules for the host along with optional constraints, and a list of routes for the path component.

翻译:

(忽略。。。。)


1
2
Host1 = { HostMatch , PathsList }.
Host2 = { HostMatch , Constraints , PathsList }.

The list of routes for the path component is defined similar to the list of hosts.

翻译:

(忽略。。。。)
1
PathsList = [ Path1 , Path2 , ... PathN ].

Finally, each path contains matching rules for the path along with optional constraints, and gives us the handler module to be used along with options that will be given to it on initialization.

1
2
Path1 = { PathMatch , Handler , Opts }.
Path2 = { PathMatch , Constraints , Handler , Opts }.

Continue reading to learn more about the match syntax and the optional constraints.

翻译:

(忽略。。。。)


HostMatch和PathMatch应该就是匹配Host和Path时的规则。

Path的定义中的Handle部分指明了Handle模块是哪个

关于HostMatch和PathMatch的写法应该就在下面。


Match syntax

(本部分介绍上面的HostMatch和PathMatch的写法,即匹配规则是如何写的)

The match syntax is used to associate host names and paths with their respective handlers.

翻译:

匹配规则就是为了让host和path与他们的处理者handle关联起来

The match syntax is the same for host and path with a few subtleties. Indeed, the segments separator is different, and the host is matched starting from the last segment going to the first. All examples will feature both host and path match rules and explain the differences when encountered.

翻译:

针对host和path的匹配规则只有一些细微的差别。事实上,比如,段落分隔符是不一样的,还有,host是从尾部的开始匹配的,一直匹配到第一个段落.

下面的全部例子中,会一起讲关于host和path的匹配规则,然后当遇到两者匹配的不同之处时,会做出解释。

Excluding special values that we will explain at the end of this section, the simplest match value is a host or a path. It can be given as either astring() or abinary().

例子:

PathMatch1 = "/" .
PathMatch2 = "/path/to/resource" .
 
HostMatch1 = "cowboy.example.org" .

翻译:

在匹配语法中的一些比较特别的值放到后面讲解,最简单的匹配值就是一个   string()类型或binary()类型     的host或path。

As you can see, all paths defined this way must start with a slash character. Note that these two paths are identical as far as routing is concerned.

PathMatch2 = "/path/to/resource" .
PathMatch3 = "/path/to/resource/" .

翻译:

PathMatch的值以一个  /  符号开头。注意,前面相同,最后相差一个 / 的PathMatch是被认为一样的。 


Hosts with and without a trailing dot are equivalent for routing. Similarly, hosts with and without a leading dot are also equivalent.

HostMatch1 = "cowboy.example.org" .
HostMatch2 = "cowboy.example.org." .
HostMatch3 = ".cowboy.example.org" .


翻译;

Hosts中首尾相差一个点 "." 都是被认为是一样的。

It is possible to extract segments of the host and path and to store the values in the Req object for later use. We call these kind of values bindings.

翻译:

可以讲host和path中的某段落给提取出来,然后讲值存储在Req对象中以备后续使用。这样的值叫做bingdings

The syntax for bindings is very simple. A segment that begins with the : character means that what follows until the end of the segment is the name of the binding in which the segment value will be stored.

PathMatch = "/hats/:name/prices".
HostMatch = ":subdomain.example.org".

If these two end up matching when routing, you will end up with two bindings defined,subdomain and name, each containing the segment value where they were defined. For example, the URLhttp://test.example.org/hats/wild_cowboy_legendary/prices will result in having the valuetest bound to the namesubdomain and the value wild_cowboy_legendary bound to the namename. They can later be retrieved using cowboy_req:binding/{2,3}. The binding name must be given as an atom.

翻译:

bindings的语法很简单。一个以冒号 :  开始的段落解释这样子的:

从冒号开始,到该段落结束的内容是binding的名字,而binding则将存储该段落的值(不知道理解对不?例如  Host = ”:A“    A = www.baidu.com  A为Erlang中的绑定,而Host要获取A的值,则加个冒号  )

当针对这两者的匹配结束时,会得到两个绑定,subdomain和name,每一个都存储了对应的一个段落值。

比如:

URL http://test.example.org/hats/wild_cowboy_legendary/prices ,值test将会绑定到subdomain,值wild_cowboy_legendary将会绑定到name,之后可以利用cowboy_req:binding/{2,3}重新得到。binding名字必须是原子类型。


There is a special binding name you can use to mimic the underscore variable in Erlang. Any match against the_ binding will succeed but the data will be discarded. This is especially useful for matching against many domain names in one go.

翻译:

在Erlang中有一个特殊的绑定变量可用,那就是  _     .

_  可以任意匹配,但是被匹配的值则被忽略了。当想一口气匹配多个域名时特别有用。

Similarly, it is possible to have optional segments. Anything between brackets is optional.

翻译:

类似的,也可以有可选段。用{ }包围的都是可选段落。

You can also have imbricated optional segments.

翻译:

(忽略。。。。)

You can retrieve the rest of the host or path using [...]. In the case of hosts it will match anything before, in the case of paths anything after the previously matched segments. It is a special case of optional segments, in that it can have zero, one or many segments. You can then find the segments using cowboy_req:host_info/1 andcowboy_req:path_info/1 respectively. They will be represented as a list of segments.

翻译:

可以用[...]得到host和path中的剩余部分。因为前面说了,host是从尾部开始匹配的,则用[...]时,则前面的剩余部分都回匹配,而对于path来说,则是后面的部分都匹配。

对于optional这一字段比较特别,它可以有零个,一个或者多个段。可以利用cowboy_req:host_info/1cowboy_req:path_info/1查找段落。他们会返回一个段落列表。

If a binding appears twice in the routing rules, then the match will succeed only if they share the same value. This copies the Erlang pattern matching behavior.

This is also true when an optional segment is present. In this case the two values must be identical only if the segment is available.

If a binding is defined in both the host and path, then they must also share the same value.

翻译:

在routing规则中,如果一个binding变量出现两次,则必须拥有相同的值,匹配才会成功。参照Erlang的模式匹配规则。

该规则在option字段也同样适用。。。。。。

在host和path中出现了同一个binding变量,则也必须拥有相同的值。

Finally, there are two special match values that can be used. The first is the atom'_' which will match any host or path.

PathMatch = '_'.
HostMatch = '_'.


The second is the special host match "*" which will match the wildcard path, generally used alongside the OPTIONS method.

HostMatch = "*".


翻译:

(忽略。。。。)

Constraints

After the matching has completed, the resulting bindings can be tested against a set of constraints. Constraints are only tested when the binding is defined. They run in the order you defined them. The match will succeed only if they all succeed. If the match fails, then Cowboy tries the next route in the list.

The format used for constraints is the same as match functions in cowboy_req: they are provided as a list of fields which may have one or more constraints. While the router accepts the same format, it will skip fields with no constraints and will also ignore default values, if any.

Read more about constraints.

翻译:

匹配结束后,binding变量可以通过一系列的约束来测试。约束 只有在binding变量被定义之后才能测试。他们以定义的顺序运行。只有全部测试成功,匹配才成功。(前面说after the matching ,,,,,现在又说才成功,不理解)如果本次匹配失败,则Cowboy会接着对下一个route进行匹配。

用于 约束 的格式跟在cowboy_req中的匹配函数是一样的:也是以一个多个域的列表存在,而域中又一个活多个约束。

尽管router接受一样的格式,但会跳过没有约束的域,也会忽视默认的值。

Compilation

The structure defined in this chapter needs to be compiled before it is passed to Cowboy. This allows Cowboy to efficiently lookup the correct handler to run instead of having to parse the routes repeatedly.

This can be done with a simple call to cowboy_router:compile/1.

Note that this function will return {error, badarg} if the structure given is incorrect.

翻译:

在本章节定义的结构在传给Cowboy之前必须先编译。这样的话,Cowboy就可以快速的找到正确的handler来处理,而不是反反复复的解析这个route列表。

可以利用cowboy_router:compile/1来完成编译工作。

若果给的route的格式不正确,将返回{error, badarg}

(那么看来,第一步构造好上面的要传入Cowboy的route后,先调用cowboy_router:compile/1,将route传进去

Live update

You can use the cowboy:set_env/3 function for updating the dispatch list used by routing. This will apply to all new connections accepted by the listener.

cowboy:set_env(my_http_listener, dispatch,
    cowboy_router:compile(Dispatch)).

Note that you need to compile the routes before updating.


翻译:

可以利用cowboy:set_env/3更新dispatch list。这将影响到listener模块所接受的全部新的连接。

注意,在更新前,还是需要先用

cowboy_router:compile
更新下



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值