`
dcaoyuan
  • 浏览: 307309 次
社区版块
存档分类
最新评论

From Rails to Erlyweb - Part II Manage Project - Reloaded

阅读更多

The migrating from Rails to Erlyweb of our project is going to be finished. I got more experience on how to deal with Erlyweb. First, the project management can be more straightforward. Here is it:

2. Manage project - Reloaded

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
  |   * erlyweb.conf
  + script
      + ebin
      + src
           * erlyweb_app.erl

Where, config/yaws.conf contains the yaws' configuration. Here's mine:

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

<server localhost>
  port = 8000
  listen = 0.0.0.0
  docroot = D:/myapp/trunk/apps/myapp/www
  appmods = </myapp, erlyweb>
        start_mod = erlyweb_app
  <opaque>
    appname = myapp
                environment = development
        </opaque>
</server>

You may have noticed, all beams under D:/myapp/trunk/script/ebin will be auto-loaded when yaws starts up. And you can prepare another yaws.conf for test or production environment by change the environment var in opaque

Now the config/erlyweb.conf:

{pa, ["script/ebin", 
      "apps/myapp/ebin", 
      "vendor/erlyweb/ebin", 
      "vendor/eunit/ebin"]}.

{i, ["vendor", 
     "apps/myapp/include",
     "/usr/local/lib/yaws"]}.

{production,  [{dbdriver, mysql},
               {database, "mydb_production"},
               {hostname, "localhost"}, 
               {username, "mememe"},
               {password, "pwpwpw"}]}.

{development, [{dbdriver, mysql},
               {database, "mydb_development"},
               {hostname, "localhost"}, 
               {username, "mememe"},
               {password, "pwpwpw"}]}.

{test,        [{dbdriver, mysql},
               {database, "mydb_test"},
               {hostname, "localhost"}, 
               {username, "mememe"},
               {password, "pwpwpw"}]}.

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.

Notice: erlyweb 0.6.2 needed, which contains Haoboy's logfun patch.

%% @doc Main entrance to the entire erlyweb application.
-module(erlyweb_app).

-export([start/1]).

-export([get_conf/1,
         build/1,
         build_test/1, 
   build_product/1,
   environment/1,
   decompile/2,
   db_log/4,
   db_dummy_log/4
        ]).

-include("yaws/include/yaws.hrl").
-include("yaws/include/yaws_api.hrl").

db_log(Module, Line, Level, FormatFun) ->
    mysql:log(Module, Line, Level, FormatFun).
db_dummy_log(_Mod, _Line, _Level, _FormatFun) ->
    empty.

%% @doc call back function 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
%%          <opaque> context.
start(SConf) ->
    Opaque = SConf#sconf.opaque,
    AppName = proplists:get_value("appname", Opaque),
    Environment = list_to_atom(proplists:get_value("environment", Opaque)),
    {_I, Pa, Pz, Dbdriver, Database, Hostname, Username, Password} = get_conf(Environment),
    {ok, Cwd} = file:get_cwd(),
    error_logger:info_msg("CWD: ~s~n", [Cwd]),
    add_code_path(Pa, Pz),
    LogFun = 
        case Environment of
            undefined -> 
    fun erlyweb_app:db_log/4;
      production ->
    fun erlyweb_app:db_dummy_log/4;
            development ->
                %code:add_pathz("../apps/ewp/src/test"),
    fun erlyweb_app:db_log/4;
      test ->
    fun erlyweb_app:db_log/4
        end,
    error_logger:info_msg("Starting app <~s> as <~s> using database <~s>~n", 
        [AppName, Environment, Database]),    
    start_db(Dbdriver, Database, Hostname, Username, Password, LogFun).

add_code_path(Pa, Pz) ->
    AddedPa = [{Dir, code:add_patha(Dir)} || Dir <- Pa],
    AddedPz = [{Dir, code:add_pathz(Dir)} || Dir <- Pz],
    error_logger:info_msg("Add code patha: ~p~n", [AddedPa]), 
    error_logger:info_msg("Add code pathz: ~p~n", [AddedPz]).  

get_conf(Environment) when is_list(Environment) ->
    get_conf(list_to_atom(Environment));
get_conf(Environment) when is_atom(Environment) ->
    {ok, Confs} = file:consult("config/erlyweb.conf"),
    I = 
        case proplists:get_value(i, Confs) of
            undefined -> [];
            IX -> IX
        end,
    Pa = 
        case proplists:get_value(pa, Confs) of
            undefined -> [];
            PaX -> PaX
        end,
    Pz = 
        case proplists:get_value(pz, Confs) of
            undefined -> [];
            PzX -> PzX
        end,
    EnvConfs = proplists:get_value(Environment, Confs),
    Dbdriver = proplists:get_value(dbdriver, EnvConfs),
    Database = proplists:get_value(database, EnvConfs),
    Hostname = proplists:get_value(hostname, EnvConfs),
    Username = proplists:get_value(username, EnvConfs),
    Password = proplists:get_value(password, EnvConfs),
    {I, Pa, Pz, Dbdriver, Database, Hostname, Username, Password}.

start_db(Dbdriver, Database, Hostname, Username, Password, LogFun) ->
    erlydb:start(Dbdriver, [{database, Database},
          {hostname, Hostname},
          {username, Username},
          {password, Password},
          {logfun,   LogFun}]).

%% This is developer's entrance to the module.  
build(AppName) ->
    io:format("Building development version of ~s.~n", [AppName]),
    build(AppName, [debug_info], development).

build_test(AppName) ->
    io:format("Building test version of ~s.~n", [AppName]),
    build(AppName, [debug_info], test).

build_product(AppName) ->
    io:format("Building product version of ~s.~n", [AppName]),
    build(AppName, [no_debug_info], production).

build(AppName, Options, Environment) when is_atom(AppName) -> 
    build(atom_to_list(AppName), Options, Environment);
build(AppName, Options, Environment) when is_list(AppName) ->
    {I, Pa, Pz, Dbdriver, Database, Hostname, Username, Password} = get_conf(Environment),
    add_code_path(Pa, Pz),
    start_db(Dbdriver, Database, Hostname, Username, Password, fun erlyweb_app:db_log/4),
    compile(AppName, Options ++ [{auto_compile, false}], I, Dbdriver).
    
compile(AppName, Options, I, Dbdriver) ->
    erlyweb:compile("./apps/" ++ AppName, lists:foldl(
                        fun(Dir, Acc) ->
        [{i, filename:absname(Dir)} | Acc]
      end, [], I) ++
            [{erlydb_driver, Dbdriver}] ++ Options).


decompile(AppName, Beam) when is_list(AppName) ->
    decompile(list_to_atom(AppName), Beam);
decompile(AppName, Beam) when is_atom(AppName) ->
    {BinFilename, SrcFilename} = 
        case AppName of 
            erlyweb -> 
          {"./vendor/erlyweb/ebin/" ++ atom_to_list(Beam),
           "./erlyweb_" ++ atom_to_list(Beam)};
      _ ->
          {"./apps/" ++ atom_to_list(AppName) ++ "/ebin/" ++ atom_to_list(Beam),
           "./apps/" ++ atom_to_list(AppName) ++ "_" ++ atom_to_list(Beam)}
        end,
    decompile_beam(BinFilename, SrcFilename).

decompile_beam(BinFilename, SrcFilename) ->
    io:format("Beam file: ~s~n", [BinFilename]),
    io:format("Source file: ~s~n", [SrcFilename++".erl"]),
    {ok, {_, [{abstract_code, {_, AC}}]}} = beam_lib:chunks(BinFilename, [abstract_code]),
    %% do not with ".erl" ext?, otherwise will be compiled by erlyweb
    {ok, S} = file:open(SrcFilename ++ ".erl", write), 
    io:fwrite(S, "~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).

To build it,

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

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 -sname myapp -i --conf config/yaws.conf --erlang "-smp auto"
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.

And if you'd like to build it from another erl shell, try this:

erl -sname erlybird
(erlybird@myhost)1> rpc:call(myapp@myhost, erlyweb_app, build, [myapp])

Yes, next version of ErlyBird will support building erlyweb apps remotely in ErlyBird's Erlang shell.

分享到:
评论

相关推荐

    rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机.zip

    rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机 用于 Ruby on Rails 核心开发的虚拟机简介注意:这个虚拟机不是为 Rails 应用程序开发而设计的,只是为。 这个项目自动设置开发环境,以便在 Ruby on Rails ...

    rails-documentation-2-0-2

    rails-documentation-2-0-2

    rails-beginner-s-guide

    rails-beginner-s-guide是Rails 指导手册,帮组学习了解rails开发

    sclo-ror42-rubygem-rails-html-sanitizer-1.0.3-1.el7.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    rails-dev-box:用于Ruby on Rails核心开发的虚拟机

    用于Ruby on Rails核心开发的虚拟机 介绍 请注意,该虚拟机并非为...host $ cd rails-dev-box host $ vagrant up 而已。 安装完成后,您可以使用以下命令访问虚拟机: host $ vagrant ssh Welcome to Ubuntu 20.10

    rails应用--导航栏实例工程

    在本项目"rails应用--导航栏实例工程"中,我们将探讨如何在Ruby on Rails框架下构建一个实用的导航栏。Rails是一个流行的开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,使得开发过程更加高效且结构...

    rails-documentation-1-2-1.zip

    标题 "rails-documentation-1-2-1.zip" 暗示这是一份关于 Ruby on Rails 框架的文档,版本为 1.2.1。Ruby 是一种面向对象的编程语言,而 Rails 是一个基于 Ruby 的开源 Web 应用程序框架,遵循 Model-View-...

    rails-documentation-1-2-0-rc1.chm

    rails-documentation-1-2-0-rc1.chm

    rails-react-components-源码.rar

    本文将深入探讨"rails-react-components-源码.rar"中的关键知识点,帮助开发者理解如何在Rails应用中集成React组件。 1. **React组件化开发** React的核心概念是组件,它允许我们将UI拆分为独立、可重用的部分。在...

    rails-ftw-v0.18-2.1.5-4.1.8

    rails-ftw-v0.18-2.1.5-4.1.8.exe用于在windows环境下搭建readmine环境

    rails-chm-2-0-2.rar

    `rails-documentation-2-0-2.chm` 文件详细涵盖了这些概念,包含了关于Rails 2.0.2的API参考、教程和指南。通过仔细阅读和实践,开发者能够深入理解Rails的工作原理,并有效地开发出高效、可维护的Web应用。

    rails-2.1.0-gem

    "rails-2.1.0-gem"是Rails框架的一个特定版本,即2.1.0的gem包,用于在Ruby环境中安装和管理Rails框架。 Rails的核心理念是“约定优于配置”(Convention over Configuration),这意味着开发者可以遵循一套预设的...

    rails-development-environment-master.rar

    标题 "rails-development-environment-master.rar" 暗示这是一个关于Ruby on Rails开发环境的压缩包。Rails是基于Ruby语言的一个开源Web应用框架,遵循MVC(Model-View-Controller)架构模式,广泛用于构建数据库...

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    - **步骤**:通过命令行使用`rails new project_name`来初始化一个新的Rails项目。 - **结构**:新项目将包含默认的目录结构,如`app`、`config`、`db`等,分别用于存放应用程序代码、配置文件、数据库迁移脚本等。 ...

    rails-yelp-mvp-源码.rar

    【标题】"rails-yelp-mvp-源码" 指的是一个基于Rails框架开发的类似于Yelp(美国知名餐饮评论网站)的最小可行产品(Minimum Viable Product, MVP)的源代码。Rails是Ruby编程语言的一个流行Web开发框架,以其“约定...

    rails-playlists-源码.rar

    "rails-playlists-源码"很可能是某个开发者或团队分享的关于音乐播放列表管理功能的示例代码或项目。在本文中,我们将深入探讨Rails框架的基本概念,以及如何通过源码理解其在实现播放列表功能时的核心技术。 1. **...

    rails-nginx-passenger-ubuntu, 关于如何在 Nginx 8.04服务器上启动和运行和乘客的说明.zip

    rails-nginx-passenger-ubuntu, 关于如何在 Nginx 8.04服务器上启动和运行和乘客的说明 rails-nginx-passenger-ubuntu我关于用 ubuntu 。Nginx 。乘客和mysql建立 Rails的简单制作服务器的笔记。别名echo"alias ll='...

    Ajax-Rails-4-AJAX-Form.zip

    Ajax-Rails-4-AJAX-Form.zip,rails 4 ajax表单示例,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载网页的情况下...

    rails-event-sourcing-example, 带有RabbitMQ的Rails 事件.zip

    rails-event-sourcing-example, 带有RabbitMQ的Rails 事件 RabbitMQ事件源示例 运行示例 1确保已经安装并运行 RabbitMQ在macosx上,你可以使用 Homebrew 安装 RabbitMQ$ brew install rabbitmq开始使用$/u

Global site tag (gtag.js) - Google Analytics