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路由模块使用的更多相关文章

  1. [翻译][erlang]cowboy handler模块的使用

    关于Cowboy Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. Handlers,用于处理HTTP请求的程序处理模块. Plain HTTP Handlers ...

  2. Erlang cowboy http request生命周期

    Erlang cowboy http request生命周期 翻译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/http_req_life/ requ ...

  3. Erlang cowboy websocket 服务器

    Erlang cowboy websocket 服务器 原文见于: http://marcelog.github.io/articles/erlang_websocket_server_cowboy_ ...

  4. Erlang cowboy 入门参考

    Erlang cowboy 入门参考 cheungmine,2014-10-28 本文翻译自: http://ninenines.eu/docs/en/cowboy/HEAD/guide/gettin ...

  5. Erlang cowboy 处理不规范的client

    Erlang cowboy 处理不规范的client Cowboy 1.0 參考 本章: Dealing with broken clients 存在很多HTTP协议的实现版本号. 很多广泛使用的cl ...

  6. Erlang cowboy 处理不规范的客户端

    Erlang cowboy 处理不规范的客户端 Cowboy 1.0 参考 本章: Dealing with broken clients 存在许多HTTP协议的实现版本.许多广泛使用的客户端,如浏览 ...

  7. Erlang cowboy 架构

    Erlang cowboy Architecture架构 Erlang cowboy参考: http://ninenines.eu/docs/en/cowboy/1.0/guide/ 本章Archit ...

  8. Erlang cowboy 处理简单的HTTP请求

    Erlang cowboy 处理简单的HTTP请求 原文出自: Handling plain HTTP requests 处理请求的最简单的方式是写一个简单的HTTP处理器.它的模型参照Erlang/ ...

  9. Erlang cowboy routing 路由

    Erlang cowboy routing 路由 本文译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/routing/ Routing 默认情况下,C ...

随机推荐

  1. ADURL简化程序

    using System; using System.Diagnostics; using System.Net; using System.Text; using System.Web; using ...

  2. python3.5------三级菜单

    笔者QQ :360212316 逻辑图 代码 city = { "华南": { "广东": ["广州市", "佛山市", ...

  3. Oracle数据库根据时间查询

    ' and trans_dt between to_date('2010-12-01 00:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('2011-12-3 ...

  4. VFP调用SOAPTOOLKIT 低级API

    Connector=CREATEOBJECT("mssoap.HttpConnector")Connector.Property["EndPointURL"] ...

  5. 关于UI系统的问题

    function OnGUI(){ GUI.skin = myskin; if(GUILayout.Button("add_component",GUILayout.Height( ...

  6. python之路-Day8

    抽象接口 class Alert(object): '''报警基类''' def send(self): raise NotImplementedError class MailAlert(Alert ...

  7. linux下redis的安装与部署及基础命令

    <1>下载安装文件:redis-3.2.5.tar.gz 放在opt目录下 <2> tar -zxvf redis-3.2.5.tar.gz,备份redis.conf到自己的目 ...

  8. 图情期刊要求2015(A,B,C类)

    中国图书馆学报+情报学报+大学图书馆学报+图书情报工作+图书情报知识+情报理论与实践+国家图书馆学刊+情报杂志+图书与情报+情报科学+图书馆杂志+图书馆建设+情报资料工作+图书馆论坛+现代图书情报技术 ...

  9. Java垃圾回收机制 入门

    对于Java虚拟机的了解,我认为是一个Java程序员已经入门的重要标志,而JVM中的垃圾回收机制(Garbage Collection,简称GC)又是JVM中的重点,所以hans在这里用篇文章时间和大 ...

  10. tesseract-ocr

    tesseract-ocr 第一课 前言 据网上介绍tesseract-ocr性能不错,并且可以支持识别中文了.于是尝试一下.   安装 1.下载地址: https://code.google.com ...