BeepBeep: A Rails like framework for Mochiweb
Dave Bryson - daveb - December 12, 2008BeepBeep is a simple web application framework for Erlang inspired by Rails and Merb. It follows the principle of convention over configuration - meaning if you follow the code structure layout and a few rules when building your app, it’ll require no extra work on you behalf to map Url requests to your Controllers and Views.
BeepBeep is built on Mochiweb and erlyDTL, providing a super fast web server and the ability to define your templates with the Django template language.
Features:
- A Script to generate a new web application (based on mochiweb’s approach)
- Session Server to store your application state
- Before filter on your controllers for things like authentication
- Django templates for the view
Getting Started:
- download the code here: BeepBeep
- CD into the beepbeep directory
- run make
- generate a new web application by running ./script/new_beep.erl YouAppName “DestinationDirectory
This will create a web app with everything you need. It includes a sample controller (main_controller.erl).
To run the sample:
- Cd into the new application’s directory you created above and start the server: ./start-server.sh
- Open a browser and visit “http://localhost:8000″
How it works:
You write a controller similar to how you’d write a “gen_server” based app, but in our case you use the included “gen_controller” behavior. In the controller you define the functions you want to expose to requests. BeepBeep will automatically map Url requests to controller and functions (or actions). For example a request to “/hello/show” would map to the “hello_controller” and invoke the “show” function.
Here’s a controller example:
%% hello_controller.erl
-module(hello_controller).
-export([show/1]).
-export([handle_request/2, before_filter/1]).
-behaviour(gen_controller).
-include("beepbeep.hrl").
show(Params) ->
gen_controller:call(?MODULE,index,Params).
%% Callback for show
handle_request(show,Params) ->
{render, "hello/show.html",[{name,"BeepBeep"}],Params}.
%% Callback filter for things like authentication (not using it here)
before_filter(Params) ->
{ok}.
From “handle_request” we return a tuple that tells the framework what template to use. Templates are located in the template directory. In our example we’ll use the template located in the subdirectory “hello” and the file “show.html”
Here’s an example of the “show.html” template:
<h2>Hello from {{ name }} </h2>
Which will result in:
<h2>Hello from BeepBeep</h2>
The “name” key set in the controller is passed to the template and expanded using the Django format via erlyDTL.
This BeepBeep approach provides a clean separation of the erlang logic in the controller and the html code in the template.
More to come…
Categories: Blogs Dave Bryson
Comments
Thanks for recommandation.
I have just followed the example, and got an error after executed ./start_server.sh
{“init terminating in do_boot”,{undef,[{helloworld,start,[]},{init,start_it,1},{init,start_em,1}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Add comment
Erlang on Twitter
» ingojaeckel (ingo jaeckel): Even more awesome, free Erlang resources http://t.co/blGINLJd
» DiTeam (Тимурка): @multybuq @ukhin руби хороший вариант :) можно даже без rails..попробуй erlang еще :)
» michelir5 (Micheli Gelatinous): @pharkmillups Still seeing it. I might just have to manually install it. The version of Erlang required by Riak is not current version in HB
» Angry_Lawyer (Tony Aldridge): @rvirding @saghul If Erlang kills you, does a supervisor automatically create a replacement of you?
» rvirding (Robert Virding): Softly I hope. RT @saghul: Slowly making progress… erlang is killing me.
» jsvd (João Duarte): RT @FrancescoC: Woot! RT @valdo404: Practical Erlang Programming at #QConLondon I want to go there
» saghul (Saúl Ibarra Corretgé): Slowly making progress… erlang is killing me.
» dlsspy (Dustin Sallings): @IbnFirnas heh. The erlang parts are still solid. The currently active alerting box is arm5, failed over from a pc that died one day.
» quercialwji2 (Quercia Quinn): @MikeSmooth_ABCs http://t.co/pPiIpTCx
» levicole (Levi Kennedy): @pharkmillups the homebrew version of erlang is the most recent version, and riak requires R14B I think.
Statistics
Number of aggregated posts: 10456
Number of comments: 1445
Most recent article: February 06, 2012
Latest comments
» simple smile on Scale means Skills: Very informative article. Pretty sure people would love to go to that place for shopping. Specially to those who are…
» simplesmile on 27 January 2012: Erlang Solutions embarks on an Erlang Embedded KTP: Your article will make the world better. Thanks again and good luck to you in your life. See you next time.simplesmile
» tandblekning easewhite on 08 February 2012: Erlang Express 3-day Course in San Francisco on 8 February: ncomprehensible to me now, but in general, the usefulness and significance is overwhelmingtandblekning easewhite