From Rails to Erlyweb - Part II

Updated Aug 23: Please see From Rails to Erlyweb - Part II Manage Project - Reloaded

Updated July 15: store the database configuration in <opaque> session of yaws.conf

Updated May 2: erlweb:compile(AppDir::string(), Options::[option()]) has option: {auto_compile, Val}, where Val is 'true', or 'false'. In case of development, you can turn on {auto_compile, true}. So, you only need to run erlyweb_app:boot(myapp) once.

2. Manage project

Erlyweb provides erlyweb:compile(App, ..) to compile the source files under app directory. To start an app, you usually should erlydb:start(mysql, ....) and compile app files first. To make life easy, you can put some scripting like code under myproject\script directory. Here's my project source tree:

myproject
  + apps
  |   + myapp
  |       + ebin   
  |       + include
  |       + nbproject
  |       + src
  |           + components
  |           + lib
  |           + services
  |       + test
  |       + www
  + config
  |   * yaws.conf
  + script
      + ebin
      + src
           * erlyweb_app.erl

Where, config/yaws.conf contains the confsiguration that will copy/paste to your real yaws.conf file. Here's mine:

## This is the configuration of apps that will copy/paste to your yaws.conf.

ebin_dir = D:/myapp/trunk/script/ebin
ebin_dir = D:/myapp/trunk/apps/myapp/ebin

<server localhost>
	port = 8000
	listen = 0.0.0.0
	docroot = D:/myapp/trunk/apps/myapp/www
	appmods = </myapp, erlyweb>
	<opaque>
		appname = myapp
		hostname = "localhost"
		username = "mememe"
		password = "pwpwpw"
		database = "myapp_development"	
        </opaque>
</server>

You may have noticed, all beams under D:/myapp/trunk/script/ebin and D:/myapp/trunk/apps/myapp/ebin will be auto-loaded by yaws.

erlyweb_app.erl is the boot scripting code, which will be used to start db connection and compile the code. Currently I run these scripts manually. I'll talk later.

-module(erlyweb_app).

-export([start/1]).

-export([main/1, 
	 boot/1, 
	 build/1, 
	 decompile/2
        ]).

-include("yaws.hrl").

%% @doc call back funtion when yaws start an app  
%% @see man yaws.conf
%%      start_mod = Module
%%          Defines  a  user  provided  callback  module.  At startup of the
%%          server, Module:start/1 will  be  called.   The  #sconf{}  record
%%          (defined  in  yaws.hrl) will be used as the input argument. This
%%          makes it possible for  a  user  application  to  syncronize  the
%%          startup  with  the  yaws  server as well as getting hold of user
%%          specific  configuration  data,  see  the  explanation  for   the
%%          

To build it,

> erlc -I /opt/local/lib/yaws/include erlyweb_app.erl

The erlyweb_app.erl is almost escript ready, but I use it as module functions currently. It's pre-compiled and erlyweb_app.beam is placed under script/ebin

So, I start myapp by steps:

cd \myproject
yaws -i -sname yaws
1> erlyweb_app:build(myapp).

The erlyweb_app.erl is almost escript ready, but I use it as module functions currently. It's pre-compiled and erlyweb_app.beam is placed under script/ebin

After I made changes to myapp, I run above erlyweb_app:build(myapp). again, then everything is up to date.

This is surely not the best way to handle the write-compile-run-test cycle, I'll improve the scripting to let starting yaws as a node, then hot-swap the compiled code to it.

It's a good experience to play with Rails, I like rake db:migrate, script, config folders of Rails. And Grails also brings some good idea to manage web app project tree. I'll try to bring them into project manager of ErlyBird.

Next part, I'll talk about the magic behind Erlyweb, and why I prefer the magic of Erlyweb to Rails.

Comments

1. malcontent -- 2007-05-01 16:00

One thing I don't like about rails is the "one project per machine" presumption it makes. Does erlyweb allow you to set up multiple projects under the same root with each one responding to a different subdomain?

2. Caoyuan -- 2007-05-01 16:00

malcontent,

I think so.

First, you can create multiple-apps under erlyweb apps directory, for instance: apps/myapp1 apps/myapp2

Second, you can configure your apps via yaws.conf as virtual servers, by giving them different 'listen = www.domain1.com', 'listen = www.domain2.com' with corresponding 'docroot = .../myapp1/www', 'docroot = .../myapp2/www' etc.

I did not test this upon real domains yet, correct me if I'm wrong.

3. malcontent -- 2007-05-01 16:00

Is there a development, testing and production yaws.conf file? For example if I am developing on windows and deploying to linux or mac I might have different paths to erlang bin directory.

4. Caoyuan -- 2007-05-01 16:00

malcontent,

Not yet. Currently, you need to switch the dev, test, production environment manually . But, we can add these features via Erlware, which uses json as the configuration files format.

5. El Mocomabo -- 2007-05-01 16:00

I think copying Rail's horrible direcotry strucutre and "philosophy" (if it can even be called that) is a mistake from the git-go .

I wish ErlyWeb? would build on the strengths of Erlang rather than just "following" the rails MVC fad. I hope they are not trying to attract the rails crowd because believe me, its no picnic and you will regret it!

Anyhoo, the only thing rails really offers is a little moresensible way of organizing your code, but lately due to proliferation of genration, things are getting out of hand.

Maybe ErlyWeb? devs (mainly Yariv right now) can be inspired by it but not follow it (rails) to the hilt.

6. Pierre R -- 2007-05-03 16:00

I have trouble using the standalone erlybird plugin on linux. The script seems full of mistakes. I have tried to solve some of them but it cascades everywhere. I have a strange error with a meta character (I need to copy/paste another #!/bin/sh and then I have lot of errors with closing strings. These ones are real script error I would think.

If you have some time to look at your unix script file ...

Looks promising.

Thanks

7. Zigo -- 2007-05-04 16:00

Caoyuan,

What is meant by hot-deploying (?) in Erlang? Isn't it the same things as in Ruby (modify code, F5, see changes)?

Also, isn't the scalability problems of Ruby due to its inability to talk to more than one database, and not threading, etc. issues?

Cheers, Zigo

8. 思迷思 -- 2007-05-05 16:00

http://tsking.cn/ 建议大家下载使用,绝不逊于一般国外工具,其中的策略部分更具中国特色。 其中实现了将数据通过ODBC导入专业数据库的功能。 (受兄的影响,我们也在进步啊)

9. Caoyuan -- 2007-05-08 16:00

Pierre,

escript source file is not supported yet, I'll support it in next release.

10. anonymous -- 2007-05-08 16:00

Zigo,

Hot-deploying in Erlang means a lot of things. In Erlang, you can run an Erlang application as a "node", which is actually a standalone vm, there can be a lot of nodes in your local computer or remote hosts. Then, you can hot-deploying new code to any local/remote nodes from any trusted nodes.

11. Caoyuan -- 2007-05-08 16:00

思迷思,

我有空会去看看你们的工作。

Add New Comment