[翻译][erlang]cowboy路由模块使用
Cowboy是基于Erlang实现的一个轻量级、快速、模块化的http web服务器。
本文官方原文:http://ninenines.eu/docs/en/cowboy/1.0/guide/routing/
默认情况下,Cowboy不会做什么事情。
为了使Cowboy可用,需要映射URL和处理请求的Erlang模型(Module),这个过程,我们称之为路由(routing)。
当Cowboy接收到一个请求,通过路由,Cowboy就会尝试去匹配到相应请求的主机和资源路径。如果匹配成功,那么相关的Erlang代码就会被执行。
每个主机会给出相应的路由规则。Cowboy首先会匹配主机,然后尝试寻找匹配的路径。
在使用Cowboy之前需要先编译路由。
1. Structure(结构)
路由一般定义成以下结构
Routes = [Host1, Host2, ... HostN].
每个主机包含匹配规则(HostMatch)、限制规则(非必须)(Constraints)和一个由路径组成的路由列表(PathsList)。
Host1 = {HostMatch, PathsList}.
Host2 = {HostMatch, Constraints, PathsList}.
由路径组成的路由列表与主机列表类似。
PathsList = [Path1, Path2, ... PathN].
而路径(path)包含匹配路径规则、限制规则(非必须)、处理逻辑的module和会被初始化的选项参数。
Path1 = {PathMatch, Handler, Opts}.
Path2 = {PathMatch, Constraints, Handler, Opts}.
下面内容为匹配语法和限制选项。
2. Match syntax(匹配语法)
匹配语法用来关联主机名字和相应的handler路径。
主机的匹配语法和路径的匹配语法类似,只有轻微的区别。譬如,他们分隔符是不一样的。而且主机是从最后开始匹配的,而路径是不是。
(其实说了老半天,这不就是一个普通的URL嘛。URL的前半部分为主机IP或域名,这里称之为HOST,即主机。而后半部分为路径)
除了特殊情况,最简单的匹配就是只有主机或者只有路径的匹配。他的值可以为string() 或binary() 类型。
PathMatch1 = "/".
PathMatch2 = "/path/to/resource".
HostMatch1 = "cowboy.example.org".
正如你所见,所有的路径都是由斜杠开始的。注意,下面两条路径对于路由而言是一样的。
PathMatch2 = "/path/to/resource".
PathMatch3 = "/path/to/resource/".
而对于主机名,最后有点和没有点对于路由来说也是一样的。同样,在前面多一个点和少一个点也是一样的。
HostMatch1 = "cowboy.example.org".
HostMatch2 = "cowboy.example.org.".
HostMatch3 = ".cowboy.example.org".
因此能够提取主机和路径的数据段并且存储在Req 对象供后面使用。我们称之为值绑定。
绑定语法非常简单。由冒号字符(:)开头,一直到数据段的结尾的这个数据段是我们的绑定名称,会被保存。
PathMatch = "/hats/:name/prices".
HostMatch = ":subdomain.example.org".
如果这两个最终匹配,那么就会有两个绑定定义,分布是:subdomain 和:name ,每个包含被定义的数据段。例如,这个URL地址 http://test.example.org/hats/wild_cowboy_legendary/prices 会将 test绑定到subdomain ,并将wild_cowboy_legendary 绑定到 name 。他们通过cowboy_req:binding/{2,3} 函数检索出来的。绑定名字必须是原子(atom)类型。
还有一种特殊的绑定名字,它模仿erlang的下划线变量。任意内容都能与下划线(_)相匹配,但是数据会被丢弃。最有用的场景就是去匹配多个域名。
HostMatch = "ninenines.:_".
类似地,也可以添加可选内容。中括号内的内容都是可选的。
PathMatch = "/hats/[page/:number]".
HostMatch = "[www.]ninenines.eu".
并且可选内容可以内嵌
PathMatch = "/hats/[page/[:number]]".
还可以使用[...] 来获取主机名或路径剩余的部分。匹配主机的时候,需要放在最前面;匹配路径的时候是放在最后面。分别使用cowboy_req:host_info/1 和 cowboy_req:path_info/1 函数可以找到他们。
PathMatch = "/hats/[...]".
HostMatch = "[...]ninenines.eu".
如果一个绑定变量出现了两次,那么只有这两个位置的值相同的时候才会匹配成功。
PathMatch = "/hats/:name/:name".
在可选变量里面也是一样的,在下面这个例子中,如果可选变量有值,必须两个绑定变量的值都一样才可匹配到。
PathMatch = "/hats/:name/[:name]".
如果一个绑定变量出现在主机名和路径当中,他们需要是相同的才能匹配。
PathMatch = "/:user/[...]".
HostMatch = ":user.github.com".
当然也有两种特殊情况,第一种使用下划线变量(_)可以匹配任意的主机名和路径。
PathMatch = '_'.
HostMatch = '_'.
第二种,使用通配符星号(*)来匹配。
HostMatch = "*".
3. 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.
They are always given as a two or three elements tuple, where the first element is the name of the binding, the second element is the constraint's name, and the optional third element is the constraint's arguments.
The following constraints are currently defined:
- {Name, int}
- {Name, function, fun ((Value) -> true | {true, NewValue} | false)}
The int constraint will check if the binding is a binary string representing an integer, and if it is, will convert the value to integer.
The function constraint will pass the binding value to a user specified function that receives the binary value as its only argument and must return whether it fulfills the constraint, optionally modifying the value. The value thus returned can be of any type.
Note that constraint functions SHOULD be pure and MUST NOT crash.
4. Compilation(编译/收集)
在传递给Cowboy之前,定义的结构首先要先编译。才能是Cowboy有效查找到正确的handler,并执行,而不必重复地解析路由。
编译通过调用cow_router:compile/1 函数进行。
Dispatch = cowboy_router:compile([
%% {HostMatch, list({PathMatch, Handler, Opts})}
{'_', [{'_', my_handler, []}]}
]),
%% Name, NbAcceptors, TransOpts, ProtoOpts
cowboy:start_http(my_http_listener, 100,
[{port, 8080}],
[{env, [{dispatch, Dispatch}]}]
).
注意,如果结构不正确,函数会返回{error, badarg}。
5. Live update(热更新)
使用cowboy:set_env/3 函数可以更新当前的路由列表。这会应用到所有的监听器中。
cowboy:set_env(my_http_listener, dispatch,
cowboy_router:compile(Dispatch)).
注意,设置之前还是需要编译的哦。
[翻译][erlang]cowboy路由模块使用的更多相关文章
- [翻译][erlang]cowboy handler模块的使用
关于Cowboy Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. Handlers,用于处理HTTP请求的程序处理模块. Plain HTTP Handlers ...
- Erlang cowboy http request生命周期
Erlang cowboy http request生命周期 翻译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/http_req_life/ requ ...
- Erlang cowboy websocket 服务器
Erlang cowboy websocket 服务器 原文见于: http://marcelog.github.io/articles/erlang_websocket_server_cowboy_ ...
- Erlang cowboy 入门参考
Erlang cowboy 入门参考 cheungmine,2014-10-28 本文翻译自: http://ninenines.eu/docs/en/cowboy/HEAD/guide/gettin ...
- Erlang cowboy 处理不规范的client
Erlang cowboy 处理不规范的client Cowboy 1.0 參考 本章: Dealing with broken clients 存在很多HTTP协议的实现版本号. 很多广泛使用的cl ...
- Erlang cowboy 处理不规范的客户端
Erlang cowboy 处理不规范的客户端 Cowboy 1.0 参考 本章: Dealing with broken clients 存在许多HTTP协议的实现版本.许多广泛使用的客户端,如浏览 ...
- Erlang cowboy 架构
Erlang cowboy Architecture架构 Erlang cowboy参考: http://ninenines.eu/docs/en/cowboy/1.0/guide/ 本章Archit ...
- Erlang cowboy 处理简单的HTTP请求
Erlang cowboy 处理简单的HTTP请求 原文出自: Handling plain HTTP requests 处理请求的最简单的方式是写一个简单的HTTP处理器.它的模型参照Erlang/ ...
- Erlang cowboy routing 路由
Erlang cowboy routing 路由 本文译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/routing/ Routing 默认情况下,C ...
随机推荐
- python Tab自动补全命令设置
Mac/Windows下需要安装模块儿 pip install pyreadline pip install rlcompleter pip install readline 注意,需要先安装pyre ...
- maven使用阿里云仓库
1.修改maven的conf/settings.xml文件,在<mirrors></mirrors>标签里加入: <mirror> <id>nexus- ...
- DataRow循环取出
string strName = null; DataRow dRow = DB.GetDataRow(Sql语句); if (null != dRow) { foreach (DataRow ite ...
- Xcode 7.2.1 下载地址
Xcode 7 下载地址(需要登录苹果开发者帐号) 7.2.1:https://developer.apple.com/services-account/download?path=/Develope ...
- Visual Studio 2013 always switches source control plugin to Git and disconnect TFS
A few days ago, I've been facing a strange behavior with Visual Studio 2013. No matter what solu ...
- Python实现插件机制——自动import一个目录下的所有.py文件
假设有这样一个目录结构: /src main.py /plugins __init__.py a.py ...
- 关于VS快捷键的使用总结
首先快捷键是为了快,不能为了用快捷键而用快捷键. 既然说到快,我个人认为要涉及到一个使用频率问题,如果一个功能的快捷键使用频率极高,那么就应该把它调到触手可及的位置, 如:经常看别人源代码,经常会使用 ...
- Redis从基础命令到实战之字符串类型
字符串类型是Redis中最基本的数据类型,能存储任何形式的字符串和和二进制数据.本文以代码形式列举常用的操作命令,并在实践部分演示一个简单的商品管理功能,实现了通常使用关系型数据库开发的增改查功能,注 ...
- Oracle 更新表(另一张表)
Update a set(a.province,a.city)= (select province,city from b where b.mobile=a.mobile)
- Netezza SQL Analytic Functions 分析函数
应用场景: 分组排序,分组累加求和... 基本语法: Func( value_expression) OVER ( [<partition_by_clause>] [<order_b ...