ChicagoBoss简介

作者: 懿民 | 来源:发表于2015-04-10 13:49 被阅读652次

ChicagoBoss简介

翻译人:李罗琦

(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简介

    ChicagoBoss简介 翻译人:李罗琦 (ChicagoBoss)[http://www.chicagobos...

  • Introduction to ChicagoBoss

    Introduction to ChicagoBoss: (ChicagoBoss)http://www.chic...

  • 18旅游二班各组自我简介汇总

    一组简介 二组简介 三组简介 四组简介 五组简介 六组简介 七组简介 八组简介

  • 简介

    上辈子,洛幽喜欢了东凡一辈子,没有结果,之后她死了,她不后悔,只是为什么她一睁眼,嗯?捏了捏自己的脸,她这是回到了...

  • 简介

    1.主动地去阅读。 2.阅读目的:获得资讯,获得理解 前者为了增进资讯,获得信息,不能增加理解力。后者为了理解更多...

  • 简介

    姓名:赵金星 电话:13903984903 坐标:河南三门峡市陕州区。 三个标签的自我介绍: 1.时间管理践行者 ...

  • 简介

    朱凝不过打错个字,便被天打雷劈回古代。 为毛别人穿个废柴嫡女,她穿个废柴庶女?地位更低了哎 哇喔,别人嫁王爷,她嫁...

  • 简介

    凌波网络考试系统,是一套运行于Windows系统之上的无纸化网络考试软件。包括试卷编辑、分发试卷、学生答卷、自动阅...

  • 简介

    名字:笨小孩 不是富二代的笨小孩为了生活选择了编程。在大多数人眼里可能感觉编程是很无聊很辛苦经常加班的事情,其实人...

  • 简介

    A Native Client web application consists of JavaScript, H...

网友评论

    本文标题:ChicagoBoss简介

    本文链接:https://www.haomeiwen.com/subject/aqdtfttx.html