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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市句狼,隨后出現(xiàn)的幾起案子枕荞,更是在濱河造成了極大的恐慌酣衷,老刑警劉巖寒波,帶你破解...
    沈念sama閱讀 207,248評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饥悴,死亡現(xiàn)場離奇詭異夺巩,居然都是意外死亡枝嘶,警方通過查閱死者的電腦和手機(jī)合住,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評論 2 381
  • 文/潘曉璐 我一進(jìn)店門绰精,熙熙樓的掌柜王于貴愁眉苦臉地迎上來撒璧,“玉大人,你說我怎么就攤上這事笨使∏溆#” “怎么了?”我有些...
    開封第一講書人閱讀 153,443評論 0 344
  • 文/不壞的土叔 我叫張陵阱表,是天一觀的道長殿如。 經(jīng)常有香客問我,道長最爬,這世上最難降的妖魔是什么涉馁? 我笑而不...
    開封第一講書人閱讀 55,475評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮爱致,結(jié)果婚禮上烤送,老公的妹妹穿的比我還像新娘。我一直安慰自己糠悯,他們只是感情好帮坚,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,458評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著互艾,像睡著了一般试和。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上纫普,一...
    開封第一講書人閱讀 49,185評論 1 284
  • 那天阅悍,我揣著相機(jī)與錄音,去河邊找鬼昨稼。 笑死节视,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的假栓。 我是一名探鬼主播寻行,決...
    沈念sama閱讀 38,451評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼匾荆!你這毒婦竟也來了拌蜘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,112評論 0 261
  • 序言:老撾萬榮一對情侶失蹤棋凳,失蹤者是張志新(化名)和其女友劉穎拦坠,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體剩岳,經(jīng)...
    沈念sama閱讀 43,609評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡贞滨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,083評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晓铆。...
    茶點(diǎn)故事閱讀 38,163評論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡勺良,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出骄噪,到底是詐尸還是另有隱情尚困,我是刑警寧澤,帶...
    沈念sama閱讀 33,803評論 4 323
  • 正文 年R本政府宣布链蕊,位于F島的核電站事甜,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏滔韵。R本人自食惡果不足惜逻谦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,357評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望陪蜻。 院中可真熱鬧邦马,春花似錦、人聲如沸宴卖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,357評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽症昏。三九已至随闽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肝谭,已是汗流浹背橱脸。 一陣腳步聲響...
    開封第一講書人閱讀 31,590評論 1 261
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留分苇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,636評論 2 355
  • 正文 我出身青樓屁桑,卻偏偏與公主長得像医寿,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子蘑斧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,925評論 2 344

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,399評論 0 23
  • 每一條道路都必須自己走出來靖秩,就像面對一條河流,成功者和失敗者在同一個(gè)事件里獲得的東西完全不一樣竖瘾,水牛會告訴你水很淺...
    飯飯_Rice閱讀 80評論 0 0
  • 發(fā)現(xiàn)兩端思維處處有沟突,似乎思維定勢,我們深受其害捕传,折磨自己惠拭,折磨他人。時(shí)不時(shí)的要保持清醒,不陷入執(zhí)念职辅。不要讓這些人本...
    我就是那片云閱讀 229評論 0 0
  • 序 Do you know who he is? 今天一篇標(biāo)題為“年輕男子在高檔小區(qū)內(nèi)死亡棒呛,尸體幾日后才被發(fā)現(xiàn)”的...
    象_閱讀 187評論 0 0