Introduction to ChicagoBoss

Introduction to ChicagoBoss:

(ChicagoBoss)http://www.chicagoboss.org] was made by (Evan Miller)[],
it provide to erlang folk a MVC pattern web framework like Ruby On Rail,
to develop scalable web app using Erlang/OTP.
Erlang like Java is a compiled language, an erlang source file is compiled to a beam file.
an run inside the Erlang Virtual Machine like Java compile to bytecode and run inside the JVM
(java virtual machine).
in this serie of article i will talk about ChicagoBoss,
i assume you know erlang or have some knowledge about fonctional programming,
if not i suggest you to visit learnyousomeerlang.org.

Step By Step:

Install erlang:

on linux Ubuntu 14.04, i usualy remove pre installed erlang runtime and use kerl to install erlang from source.

> 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

after this step, you have a working erlang runtime compiled from source.
just type erl in your shell and you will get the erlang REPL.

Install ChicagoBoss:

>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

Create my first CB project and start developping.

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

anatomy of an CB app source tree.

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 is the config file of your application.
  • init-dev.sh start your app in developping, auto reload and recompilation.
  • init.sh start your app
  • deps, here goes all your dependancy application
  • log, log file of your application
  • priv/first.routes , custom routing file
  • priv/init , here goes your init script
  • priv/static, here goes your static files
  • src/controller, yours app controllers source.
  • src/mail, incoming/outgoing mail controller
  • src/view//.html
  • src/websocket, websocket controller.

Which IDE?

personnaly is use Emacs, some people use Sublime Text, some Erlide, some VIM.
it is up to you, any text editor is fine.

My first controller/view.

convention, a controller is named like this:
/src/__controller.erl

for example i want to create a controller with the name index,
with an action call index,
when the browser go to http://localhost:8001/index/index my app should show
Hello world text.
edit first/src/controller/first_index_controller.erl like

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

the corresponding view:
/src/view//.

for the action index in controller index.
first/src/view/index/index.html

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

action:

an action in CB is a function with 2 or 3 parameters.

  • the first parameter is the method matching the requested method, it is an atom:
    'GET', 'POST', 'PUT', 'DELETE', 'HEAD' ...
  • the second parameter is uri token.
  • the third parameter is optional: Request Context, CB will provide to your action
    the request context, it is a proplist of usefull value. this list of usefull value can be modified
    by the _before fonction or by some boss_filter fonction.
-module(first_index_controller, [Req, SessionId]).
-export([index/3]).
index('GET', [], ReqCtx) ->
lager:info("Request Context: ~p",[ReqCtx]),
{ok, [{msg, "Hello World!!!"}]}.

default action.

what is default action?,
no action cannot be guessed from the requested url,
if you want to provide such feature, you can use the attributes *-default_action().

in your controller module.
if no default action is provided, CB will redirect throw an 404 not found.

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!!!"}]}.

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

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

你可能感兴趣的:(Introduction to ChicagoBoss)