• 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.

  • 相关阅读:
    C语言volatile解析
    使用中断开关实现全局变量互斥访问
    CentOS7下gogs安装总结
    iOS Xcode: linker command failed with exit code 1 (use -v to see invocation) 处理方法
    C# 10进制与62进制互转 数据大无压力 10进制与72,96进制任意转换
    Web网站的性能测试工具
    NetCore跨平台桌面框架Avalonia的OSX程序打包
    C# 类中静态变量静态构造函数执行顺序
    mac osx下虚拟主机配置
    React+ES6+Webpack深入浅出
  • 原文地址:https://www.cnblogs.com/vowei/p/4417429.html
Copyright © 2020-2023  润新知