Tag: erlday

Erlang for everyday use (3)

Posted by on 25-Jun-2007

Feedback

None. Oops, one comment from Shaun, thank you.

Updates

Since my previous post I found a little bug in the CEAN installation. Or maybe it’s just not very well documented thing:

After the installation the path to the control socket is pointing by default to /usr/local/var :

=ERROR REPORT==== 25-Jun-2007::10:37:24 ===
Failed to create/manipulate the ctlfile
called /usr/local/var/run/yaws/ctl-default
Either problems with permissions or  earlier runs of yaws
with the same id  <default> as this, check dir for perms
None of Yaws ctl functions will work

Seems during the compilation you can control where the control socket will be via VARDIR environment variable, but we installing from binaries, so…!? I couldn’t find a way to set this path during the run time.

For me I solved the problem by recompiling the ${CEAN}/erlang/lib/yaws-1.68/src/yaws_generated.erl and replace the original ebin/yaws_generated.beam file:

$ cd ${CEAN}/erlang/lib/yaws-1.68/src/
%% correct the paths inside the yaws_generated.erl
%% to point to the CEAN install directory
$ erlc yaws_generated.erl
$ mv yaws_generated.beam ../ebin

OK, let’s go back to the today’s topic

Erlang project directory structure

Like all we know:

“Convention over Configuration” means a developer only needs to specify unconventional aspects of their application.Wikipedia, Ruby On Rails

What is the typical erlang project structure?

  • ebin/ – directory with compiled .beam files – in general you need to deploy only them to the remote location
  • src/ – directory with source .erl files
  • include/ – directory with .hrl files, defining macros, records etc.
  • priv/ – application specific configuration, docroot for the yaws-based projects

And because I know you are lazy here is an escript to automate the erlang project directory creation . Usage:

$ wget -O ~/bin/rails http://files.zhekov.net/erlday.txt && chmod 755 ~/bin/rails
$ rails ~/Work/erlang/erl_first

Oops, mistake. Better name it erlday , not rails, you still need your good old Rails, do you? ;)

How can you recompile all your sources? What about the Makefile? Or Rakefile? Good news for the Windows guys: You just don’t need all of them!

1. On the top of your project directory create a file with name Emakefile and content:

{"src/*", [debug_info, {outdir, "ebin"}, {i,"include"}]}.

2. Start the erlang shell and type make:all().

$ erl
...
1> make:all().

3. There is no step 3 ;)

Note: If you are using the erlday escript , it will create automatically the Emakefile for you.

How to find where are make module sources:

$ erl
...
1> code:which(make).
"/home/erl/cean/erlang/lib/tools-2.5.4/ebin/make.beam"

so it will be …/src/make.erl .

Practice – comet chat with yaws

And because only reading is not good, here something to play with – Comet-based, yaws-powered chat.

WARNING! The Comet application sources belong to Mikage-san . I only changed the messages to be in English. And because there was nothing mentioned about the licensing, I put a link with the original sources in the beginning of the files.

Get the erl_comet tarball and uncompress it

$ mkdir ~/Work/erl_comet && cd ~/Work/erl_comet
$ wget http://files.zhekov.net/erl_comet.tgz && tar xvzf erl_comet.tgz

Adjust the paths to the installation directory

  • src/chat.erl – path to the yaws.hrl file
  • priv/yaws.conf – path to the log directory, address, port, and path to docroot.
  • priv/docroot/index.yaws – the URL to your template file

Recompile everything and quit the erlang shell session (Ctrl+G)

$ erl
...
1> make:all().
Recompile: src/chat
up_to_date

Note: Specially for that project I included a Makefile, so you can use make and make run .

Start the application

$ make run
...
1>
=INFO REPORT==== 25-Jun-2007::11:56:31 ===
Yaws: Using config file priv/yaws.conf
yaws:Add path "/home/erl/cean/erlang/lib/yaws-1.68/examples/ebin"
yaws:Running with id="comet"
Running with debug checks turned on (slower server)
Logging to directory "/home/erl/cean/var/log"

=INFO REPORT==== 25-Jun-2007::11:56:31 ===
Yaws: Listening to 127.0.0.1:4080 for servers
 - http://localhost:4080 under /home/erl/Work/erlang/erl_comet/priv/docroot

=INFO REPORT==== 25-Jun-2007::11:56:31 ===
sync call chat:start

Point your browser to URL http://localhost:4080/ and start chatting.

Next time – inets, pico and yaws deployment. Or something else, if there are requests…

I’ll be in vacation for about 10 days, so be patient… ;)

Erlang for everyday use (2)

Posted by on 14-Jun-2007

Common Crap

After the first article there was more then 1500 hits. And interesting, maybe about 80% of them coming from RubyCorner ! Feedback: near to zero – just several comments on the blog and on reddit . Nothing to improve? Nothing to add? Hm, so it’s real:

Web 2.0 is about voting. It make you stupid!

OK. Cut the crap and let’s go on the topic

Install yaws

Hopefully you already have the CEAN installed . Next good thing is to install yawsHTTP high performance 1.1 webserver particularly well suited for dynamic-content webapplications (something like a mongrel for Erlang ;) )

1> cean:install(yaws).
+ ibrowse md5=<<212,78,39,29,222,40,222,24,91,65,162,253,27,109,180,42>>
+ compiler md5=<<19,177,55,189,37,11,164,23,75,115,129,152,51,24,167,234>>
+ inets md5=<<41,196,119,166,237,121,195,180,130,134,225,129,36,167,207,153>>
+ ssl md5=<<75,245,109,43,92,129,104,171,158,38,157,214,21,117,107,3>>
+ tools md5=<<15,253,151,211,133,14,99,68,114,47,193,244,20,251,111,76>>
+ xmerl md5=<<60,100,38,136,168,113,16,236,239,184,152,155,148,148,37,112>>
+ xmlrpc md5=<<158,3,240,16,89,215,193,84,48,248,191,58,98,30,54,53>>
+ yaws md5=<<88,53,90,204,22,201,30,118,182,116,117,36,67,251,39,191>>
ok

Create var/log , var/run and var/www INSIDE YOUR CEAN directory.

$ cd ~/cean
$ mkdir -p var/lib var/run var/www

Download the example config file and the yaws startup script (they are just modified versions from the Erlang sources, you can do this by yourself)

$ cd ~/cean
$ wget -O yaws.conf http://files.zhekov.net/yaws.conf.txt
$ cd ~/bin
$ wget -O yaws http://files.zhekov.net/yaws.txt && chmod 755 yaws

Correct the paths in the both files (##CEANDIR## , $CEANDIR, your $OSDIR – the default is linux-x86). Start yawsing

$ yaws -i -c ~/cean/yaws.conf
Erlang (BEAM) emulator version 5.5.4 [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.5.4  (abort with ^G)
1>
=INFO REPORT==== 14-Jun-2007::12:45:01 ===
Yaws: Using config file /home/erl/cean/yaws.conf
yaws:Add path "/home/erl/cean/erlang/lib/yaws-1.68/ebin"
yaws:Running with id=default
Running with debug checks turned on (slower server)
Logging to directory "/home/erl/cean/var/log"

=INFO REPORT==== 14-Jun-2007::12:45:01 ===
Yaws: Listening to 127.0.0.1:8080 for servers
 - http://localhost:8080 under /home/erl/cean/var/www

Next time: Erlang project directory structure and “makefile” a.k.a “Convention over Configuration”.

Erlang for everyday use (1)

Posted by on 12-Jun-2007

After the last Osaka Erlang meeting , decided to start a column of articles for Erlang everyday use. Recently there is a big interest in Erlang, but the beginning is always difficult. Some push during the first steps usually helps. OK, on the topic:

Installation

I hear several time: “It’s take me 30 min to compile it…”, “The PLT compile takes … min…”, “On Windows it’s difficult…”. What are you doing guys?

CEAN 1.2 is online, with 200 packages.
Binary packages are available for up to 17 platforms.

Just download the binaries for your platform and you are ready. There are two flavors:

  • “Production” – only .beam files, small size, ready for production use
  • “Developer”.beam files AND sources (.erl) – go with this for your development machine

Plus you get package management (install, uninstall, dependencies tracking) for free:

[~/cean]$ ./start.sh
Erlang (BEAM) emulator version 5.5.4 [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.5.4  (abort with ^G)
1> cean:installed().
["cean",
 "compiler",
 "debugger",
 ...
 "yaws"]
2> cean:install("gs").
+ gs md5=<<241,57,155,224,152,243,10,41,231,159,223,113,196,45,116,195>>
ok
3> cean:uninstall("snmp").
can not uninstall snmp. ["mnesia"] are dependent upon it.
ok

By default the installer and the archive both install everything in one directory. Usage is through the included start.sh shell script (not sure how is on Windows).

What to use it from other directories?

Easy, just a thin wrapper will fix this:

$ cat ~/bin/erl
#!/bin/sh

CEANDIR=/home/erl/cean
${CEANDIR}/start.sh -pa ${CEANDIR}/lib/*/ebin "$@"

Missing erlc (compiler)?

Just add -compile to the arguments of the script above (some alias will do the job too):

$ erl -compile processes.erl

Need to_erl, run_erl

(for example for the yaws start/stop script)? Add the erts bin directory to your path:

$ cat .bash_profile
PATH=~/cean/erlang/erts-5.5.4/linux-x86/bin:"${PATH}"

Happy erlanging. Hm, what to post about the next time? Some ideas:

  • project directories and “makefile”
  • deployment – inets, pico, yaws

Stay tuned…