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.

你可能感兴趣的:(mysql,Web,Rails,grails,idea)