Thursday, 17 September 2009

First attempt at an Erlyweb application using YAWS and MySql

After a couple of days reading and re-reading the erlyweb blogs and posting to StackOverflow I have finally got my first Erlyweb app up and running. Before I foget it all and have to go through the same trouble again I am going to record all the steps and issues I came across.

I am working on a windows box and I am guessing this is the main reason I had so many problems together with the fact that I only started learning Erlang two weeks ago.

Anyway, this is what I did.

I was working from Yariv's Erlyweb music blog and had already downloaded Erlang, Erlyweb and YAWS. I placed the precompiled erlyweb library into the erlang lib directory and installed YAWS using the windows installer. This placed YAWS into the program files directory, which caused me many issues (however easy to overcome)

To get the music app to work after following the instructions exactly from Yariv's blog I had to add the following lines to my yaws.conf file, found in the root of the YAWS installation directory.
ebin_dir="C:\Program Files\erl5.7.2/lib/erlyweb-0.7.1/ebin"
ebin_dir="C:/Program Files/Yaws-1.84/applications/music/ebin"
The first line tells YAWS where to find the erlyweb library and the second line tells it where to find my application ebin folder containing all the compiled erlang for my application.

After adding these two lines I was able to navigate to http://localhost:8000/music/musician/new and saw a nicely generated input form for a new musician, however when I tried to save that form I got a nasty YAWS error that I didn't really understand apart from I new it had something to do with accessing the MySql db. I eventually realised that I was running my MySql process in a seperate shell to YAWS.

So in my YAWS shell I entered from following
12> erlydb:start(mysql, [
{hostname, "localhost"},
{username, "musicuser"},
{password, "password"},
{database, "music"}
]).
and voila it all works, I can create, edit, delete and list musicians and I didn't write a line of code, all created for me ina Rails scoffold style. (btw, the mysql driver does not like empty passwords so watch out for that)


In order to compile and start the MySql connection with just one command place the following file into the root of the application.
 
-module(start).
-export([boot/0, boot/1]).

boot() ->
boot(true).
boot(false) ->
compile();
boot(true) ->
mysql_start(),
compile().

mysql_start() ->
erlydb:start(mysql, [{hostname, "localhost"},
      {username, "twoorluser"},
      {password, "orgone"},
      {database, "music"}]).

compile() ->
erlyweb:compile("d:/erlang/music",
  [{erlydb_driver, mysql}]).


Compile this module c(start). 
Start yaws, change directory to the root of your app and type start:boot().

Now onto investigating mnesia and putting some of my own Erlang code into the Models, Views and Controllers.

Hope this helps someone. I will be back with my next developments very soon.