翻译人:李罗琦

(ChicagoBoss)[http://www.chicagoboss.org] 由 Evan Miller创作,使用erlang/OTP的开发者们可以通过它提供的一个类似于Ruby On Rail web框架的MVC模式去开发标准的web应用。
erlang和Java一样属于编译型语言,一个erlang的源文件会被编译成一个beam文件,beam文件可以在erlang的虚拟机里面执行就像Java编译后的字节代码在Java虚拟机JVM里面执行一样。
在这个文章的系列中,我会和大家聊一聊
ChicagoBoss。
我假定大家都了解过erlang或是掌握了一些功能性开发的知识,如果不是的话我建议大家可以去访问 learnyousomeerlang.org学习一下。

一步步来

安装erlang

系统版本:Linux Ubuntu 14.04,我通常会卸载掉预装的erlang 运行时然后去使用它的源码内核手工安装。
shell中按照下面的执行

> cd ~
> mkdir bin
> cd bin
> sudo apt-get remove erlang
> sudo apt-get build-dep erlang
> curl -O[https://raw.githubusercontent.com/spawngrid/kerl/master/kerl](https://raw.githubusercontent.com/spawngrid/kerl/master/kerl)
> chmod u+x kerl
> echo "" >> ~/.kerlrc
> ./kerl update releases
> ./kerl build 17.5 17.5
> ./kerl install 17.5 ~/bin/lang/erlang/17.5
> echo "source ~/bin/lang/erlang/17.5/activate" >> ~/.profile
> echo "export PATH=$PATH:$HOME/bin:." >> ~/.profile

上述步骤完成后,你的机器上面就有了一个可以工作的erlang运行时,你只需要在shell中输入erl就能获取到erlang REPL

安装ChicagoBoss

还是一样在shell中操作

>sudo apt-get install git
>mkdir workspace
>cd workspace
>git clone [http://github.com/ChicagoBoss/ChicagoBoss.git](http://github.com/ChicagoBoss/ChicagoBoss.git) -b v0.8.13

创建我们第一个ChicagoBoss(CB)项目并开发

还是一样子,shell命令

>cd
>cd workspace/ChicagoBoss
>make app PROJECT=first
>cd ../first
>./init-dev.sh

CB 应用源文件树结构

first/
├── boss.config
├── init-dev.sh
├── init.sh
├── deps
│ ├── boss
│ ├── boss_db
│ └── ...
├── log
│ ├── console.log
│ ├── crash.log
│ └── error.log
├── Makefile
├── priv
│ ├── first.routes
│ ├── init
│ ├── rebar
│ └── static
├── README.md
├── rebar
├── rebar.cmd
├── rebar.config
├── src
│ ├── controller
│ ├── first.app.src
│ ├── mail
│ ├── view
│ └── websocket
└── start-server.bat
  • boss.config是应用程序的配置文件.
  • init-dev.sh 启动应用的开发模式,重载和重编译。.
  • init.sh start your app 启动应用程序。
  • deps, 这里列出了你所有的依赖程序。
  • log, 程序日志文件。
  • priv/first.routes , 自定义路由信息配置文件
  • priv/init , 初始化脚本
  • priv/static, 静态文件
  • src/controller, 程序的controller控制器所在目录.
  • src/mail, 收发邮件controller控制器所在目录
  • src/view/<controller_name>/<action_name>.html 视图文件,按照控制器名称/动作名称来命名。
  • src/websocket, web套接字controller所在目录

选择哪个IDE?

我个人建议使用Emacs,有些人呢会使用Sublime Text, some Erlide或是VIM,使用哪个完全取决于你,任意一个文本编辑器就可以。

我的第一个controller/view 控制器和视图

按照惯例,一个controller的命名方式应该是这样:

/src/__controller.erl

比如说,我想创建一个名字是index的controller同时包含一个名为index的action,
当在浏览器中访问http://localhost:8001/index/index的时候会出现一行hello world的文字。
编辑
first/src/controller/first_index_controller.erl 就像下面这样:

-module(first_index_controller, [Req, SessionId]).
-export([index/3]).
index('GET', [], _ReqCtx) ->
{ok, [{msg, "Hello World!!!"}]}.

对应响应的视图文件是:
<app_name>/src/view/<controller_name>/<action_name>.<tpl_extension>

针对index controller里面的index action 视图文件是:
first/src/view/index/index.html
按照下面进行编辑:

{{ msg }}

然后
shell中通过curl命令访问:

curl -X GET[http://localhost:8001/index/index](http://localhost:8001/index/index)

action:

CB中的一个action就是一个包含2到三个参数的方法。
第一个参数是用来匹配request请求的方法, 比如:'GET', 'POST', 'PUT', 'DELETE', 'HEAD' ...
第二个参数是url的token令牌信息。
第三个参数是可选的:请求的上下文信息,CB会提供给action一个请求的上下文信息,context其实是一个键值对列表,其中的
键值对可以在一些boss_filter过滤器中的_before方法中被修改。
下面的示例给出一个带context上下文的action

-module(first_index_controller, [Req, SessionId]).
-export([index/3]).
index('GET', [], ReqCtx) ->
lager:info("Request Context: ~p",[ReqCtx]),
{ok, [{msg, "Hello World!!!"}]}.

默认action

什么是默认action,当从请求的url中无法判断出来要执行的是哪个action的时候会被转发到默认的action,如果你需要提供这样的功能你可以
在你的controller里面使用属性 *-default_action(<action_name>)。而如果你的CB程序里面没有提供默认的action,则会跳转到404页面。

举个例子
在index controller里面这样定义:

curl -X GET[http://localhost:8001/index](http://localhost:8001/index)

then http://loaclhost:8001/index will execute the default action index.

-module(first_index_controller, [Req, SessionId]).
-export([index/3]).
-default_action(index).
index('GET', [], _ReqCtx) ->
{ok, [{msg, "Hello World!!!"}]}.

通过curl命令访问

curl -X GET[http://localhost:8001/index](http://localhost:8001/index)

这个时候 http://loaclhost:8001/index 机会跳转到默认的名为index的action里面执行,页面就会出现一行文字:

hello world.

ChicagoBoss简介的更多相关文章

  1. ASP.NET Core 1.1 简介

    ASP.NET Core 1.1 于2016年11月16日发布.这个版本包括许多伟大的新功能以及许多错误修复和一般的增强.这个版本包含了多个新的中间件组件.针对Windows的WebListener服 ...

  2. MVVM模式和在WPF中的实现(一)MVVM模式简介

    MVVM模式解析和在WPF中的实现(一) MVVM模式简介 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在 ...

  3. Cassandra简介

    在前面的一篇文章<图形数据库Neo4J简介>中,我们介绍了一种非常流行的图形数据库Neo4J的使用方法.而在本文中,我们将对另外一种类型的NoSQL数据库——Cassandra进行简单地介 ...

  4. REST简介

    一说到REST,我想大家的第一反应就是“啊,就是那种前后台通信方式.”但是在要求详细讲述它所提出的各个约束,以及如何开始搭建REST服务时,却很少有人能够清晰地说出它到底是什么,需要遵守什么样的准则. ...

  5. Microservice架构模式简介

    在2014年,Sam Newman,Martin Fowler在ThoughtWorks的一位同事,出版了一本新书<Building Microservices>.该书描述了如何按照Mic ...

  6. const,static,extern 简介

    const,static,extern 简介 一.const与宏的区别: const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 执行时刻:宏是预编 ...

  7. HTTPS简介

    一.简单总结 1.HTTPS概念总结 HTTPS 就是对HTTP进行了TLS或SSL加密. 应用层的HTTP协议通过传输层的TCP协议来传输,HTTPS 在 HTTP和 TCP中间加了一层TLS/SS ...

  8. 【Machine Learning】机器学习及其基础概念简介

    机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  9. Cesium简介以及离线部署运行

    Cesium简介 cesium是国外一个基于JavaScript编写的使用WebGL的地图引擎,一款开源3DGIS的js库.cesium支持3D,2D,2.5D形式的地图展示,可以自行绘制图形,高亮区 ...

随机推荐

  1. MySQL+Navicat for MySQL安装

    一.安装MySQL 1.下载MySQL http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-winx64.zip 2.安装 2.1解压安装包 ...

  2. webservice jaxws header验证

    @WebService @HandlerChain public class UserService { ... } package com.xx.ws.header; import org.w3c. ...

  3. hdoj1251-统计难题 【字典树】

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  4. 锁机制(Lock) 信号量机制(Semaphore) 事件机制(Event)

    IPC  进程间通信(inter-Process Communicate) 锁机制(Lock) l = Lock() 开启一个锁机制(实例化)   一把锁配一个钥匙 l.acquire()  获得钥匙 ...

  5. 85. Maximal Rectangle (Graph; Stack, DP)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. UVa 12100 Printer Queue(queue或者vector模拟队列)

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  7. 浅谈python中的“ ==” 与“ is”

    在python中,== 与 is 之间既有区别,又有联系,本文将通过实际代码的演示,力争能够帮助读到这篇文章的朋友以最短的时间理清二者的关系,并深刻理解它们在内存中的实现机制.扯淡的话不多说,下面马上 ...

  8. OOP的几个不常用的方法

    from OOP_多态 import cat c = cat("cat") print(c.__doc__) print(cat.__doc__) # # 打印类的描述信息,也就是 ...

  9. MySQL基本操作之命令行操作

    MySQL基础操作 MySQL基础操作--命令行操作

  10. js验证input输入正整数 和 输入的金额小数点后保留两位(PC端键盘输入)

    // 验证开头不为零的正整数 WST.zhengZhengShuIn = function (className){ var rex = /^[1-9]{1}[0-9]*$/;//正整数 $(&quo ...