您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页ChicagoBoss简介

ChicagoBoss简介

来源:二三四教育网

ChicagoBoss简介

翻译人:李罗琦

一步步来

安装erlang

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

> cd ~
> mkdir bin
> cd bin
> sudo apt-get remove erlang
> sudo apt-get build-dep erlang
> curl 
> 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  -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

-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)
-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)

hello world.

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务