Using ErlyWeb templates with MochiWeb
Here’s a very simple way to add dynamic templates (views) to a MochiWeb based web front-end. I wouldn’t want to build an entire web application this way, but it’s a quick way to add a simple web interface to your Erlang application.
First I pulled the simple template code from ErlyWeb (erltl.erl) and compiled it with my code.
For this example, here’s a template index.et that shows a list of nodes the Erlang application is connected to.
index.et
<html> <body> <h2>Available nodes</h2> Total online: <% integer_to_list(length(Data)) %> <ul> <% [index(A) || A <- Data] %> </ul> </body> </html> <%@ index(N) %> <li><% atom_to_list(N) %></li>
Now compile it with “erltl:compile(”index.et)” and put in the same path as your mochiweb app (below).
Next setup the Mochiweb application:
-module(test_templates_web).
-export([start/1, stop/0, loop/2]).
start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
Loop = fun (Req) ->
?MODULE:loop(Req, DocRoot)
end,
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
stop() ->
mochiweb_http:stop(?MODULE).
loop(Req, DocRoot) ->
"/" ++ Path = Req:get(path),
case Req:get(method) of
'GET' ->
%% This call returns a list of nodes the Erlang application is connected to -
%% a list like this: [node1@here.com,node2@here.com]
NodeData = nodes(),
%% Pass the data to the template
Outty = index:render(NodeData),
%% Send the output (String) back to the browser
Req:ok({"text/html",Outty});
_ ->
Req:not_found()
end.
Inside the ‘GET’ above, I pass the data to the compiled template, then send it back to the browser.
Obviously I’ve glossed over a few details, but hopefully you get the idea. It may not be as “elegant” as a Rails app, but it’s blazing fast!
Install Erlang on Ubuntu with Capistrano
Here’s a simple task that I use to install Erlang on remote servers using Capistrano and Deprec. Assuming you have an SSH key setup with the remote server, you can run this task from a terminal like this: “miceda:erlang:install”. This downloads the source and does the make/make install dance.
Capistrano::Configuration.instance(:must_exist).load do
# install Erlang 12 on Ubuntu 7.10 (gutsy)
namespace :miceda do
namespace :erlang do
SRC_PACKAGES[:erlang] = {
:filename => 'otp_src_R12B-1.tar.gz',
:dir => 'otp_src_R12B-1',
:url => "http://www.erlang.org/download/otp_src_R12B-1.tar.gz",
:unpack => "tar zxf otp_src_R12B-1.tar.gz;",
:configure => %w(
./configure
;
).reject{|arg| arg.match '#'}.join(' '),
:make => 'make;',
:install => 'make install;'
}
desc "Install Erlang 12B-1"
task :install do
install_deps
deprec2.download_src(SRC_PACKAGES[:erlang], src_dir)
deprec2.install_from_src(SRC_PACKAGES[:erlang], src_dir)
end
desc "Install deps for Erlang"
task :install_deps do
apt.install( {:base => %w(libc6 libncurses5 libncurses5-dev libssl-dev openssl m4 libexpat1-dev)}, :stable )
end
end
end
end
Of course the big benefit is that with Capistrano you can do this across several servers at the same time.