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
Erlang on Twitter
» kirikaza (Kirikaza): RT @LogaRhythm: “If you don’t perform experiments before designing a new system, your entire new system will be an experiment” Mike William…
» digitalBush (Josh Bush): Lost a couple of hours figuring this one out “erl -pa ebin” needs to be “erl -pa ../{app}/ebin” if you want code:priv_dir/1 to work. #erlang
» buzzamzn (アマゾンに関する情報を発信中♪): 楽天市場:Learn You Some Erlang for Great Good!-【電子ブック版】 http://t.co/Ot4LPkbgw7 #amazon #アマゾン #相互フォロー
» rvalyi (Raphaël Valyi): @matrixise j’avais bidouillé un peu de Erlang récemment, pas trop mon truc, mais là avec la Syntaxe de Ruby/team Plataformatec, ça motive…
» imarshut (MarsHut): #Calling #Ruby #Functions http://t.co/79x1tBhoc9 ErlPort (library to connect Erlang to Ruby) 1.0.0alpha released
» fogelmania (Raphael Fogel): Alexander Fok, System Architect @ooVoo will have a talk at the #Erlang track of #DevconTLV http://t.co/GRA0Q2zny7 Welcone to TLV, Alexander!
» robertoaloi (Roberto Aloi): @mariofusco Wouldn’t it be simpler switching to #erlang? ;-)
» guillaumepotier (Guillaume POTIER): @francois2metz du coup tu vas recruter une armée de full stack ? Tu ne veux pas qques spécialistes Erlang ?
» oker1 (Zsolt Takács): Mochi Labs - statebox, an eventually consistent data model for Erlang (and Riak) http://t.co/tWhlCDGtBz
» HumphreyStamper (Humphrey Stamper): Wage reduction the trim erlang only http://t.co/6qTcEyl3Sk tactician in contemplation of organism leap http://t.co/1CWvIAjBNU
Statistics
Number of aggregated posts: 10661
Most recent article: June 19, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…