Erlang によるウェブプログラミング

Erlang は「コンピュータにおいて汎用的な用途に使うことができる並列処理指向のプログラミング言語および実行環境である」(Wikipedia) 言語としては関数型言語で、軽量プロセスを扱うのが得意と聞いている。

ベトナム語のウェブサイト cntt.tv より

Lập trình web với Erlang(Erlang によるウェブプログラミング)

を翻訳する。

==翻訳開始

Erlang によるウェブプログラミング

Python, Ruby, PHP そして Java のように、よく使用される言語では、強力な ウェブフレームワークがたくさんあります。Erlang は世に出て20年になります。ネットワークプログラミングに対してはよいサポートがありますが、ウェブ方面はまだ弱いです。複雑なビューやコントローラのあるウェブをErlang で書くのは、いまのところ間違った決定でしょう。しかしながら、すでに Erlang で書かれたシステムがあるところに、単純なウェブアプリケーションを書き足して(例えばOTP(Open Telecom Platform)モデルに従って)元のシステムと統合する必要がある場合には、Erlang でシステムを統合して、新しい部分と従来の部分の間のインピーダンス・ミスマッチを避けることは、正しい決定といえるでしょう。

私たちは、Hello World プログラムを書いて、web アプリケーションをサポートする各種ライブラリ(Yaws, MochiWeb, ErlyWeb) について調べてみましょう。(これらは、まだ framework と呼べる水準には達していません) この記事を理解するためには、Erlang に関する知識と、ウェブフレームワークに関する経験が必要です。

Yaws

わかりやすく言うと、Yaws = Apache + PHP でしょうか。つまり、Yaws はウェブサーバーと PHP 風のプログラム環境を提供するということです。卒業研究とか手作りで何かを作るといった課題で、一から自分でウェブフレームワークを作りたいとき Yaws を使うのは、よい選択です。Yaws の具体的に特徴について知りたい場合は、参考資料セクションのリストを参考にしてください。

Yaws といえば、多くの人たちに記憶にあるのは、Erlang の作者が行った Apache vs. Yaws のベンチマークテストではないでしょうか。このベンチマークテストによると、Yaws は Apache より高速で、80 リクエストを同時に処理にしたのに対して、Apache ではたった4リクエストでダウンしてしまいました。

Yaws (一般的に、Erlang と Yaws はパッケージ管理プログラムを通じてインストールします。自分でコンパイルするのではなく)をインストールすると、次のようなディレクトリ構造が作られます。

hello_yaws
  yaws.conf  
  ebin
  log
  src
    include
  public
    index.yaws

yaws.conf:

logdir = ./log
ebin_dir = ./ebin
include_dir = ./src/include

<server localhost>
  port = 8000 
  listen = 0.0.0.0
  docroot = ./public
</server>

index.yaws:

<html>
<head>
  <title>Hello from Yaws</title>
</head>
<body>
  <erl>
    out(A) ->
      {html, "Hello World"}.
  </erl>
</html>

.
Hello_yaws ディレクトリに入り、yaws -i というコマンドを叩いて、8000番ポートで Yaws を走らせます。その後 http://localhost:8000 をブラウザで表示すると Hello World の行が表示されるはずです。

MochiWeb

Yaws にはドキュメントががたくさん用意されていますが、やや原始的です。MochiWeb はより使いやすいのですが、ドキュメントはほとんど何もありません。Inet と Yaws を比較して、MochiWeb は次のように評価されています

I think of it as a programmable HTTP server layer. The inet library is a handy library, but not really a fully capable Web server. Yaws, on the other hand, is closer to Apache -- very capable, but with many of its own ideas about how to build your Web systems.


MochiWeb does the heavy lifting on your outward-facing HTTP connections, leaving you to write your application-specific server logic. It provides useful abstractions over the request and response, and manages your client connections and other details of pushing data between your code and your clients. It doesn't do too much for you beyond that, which is actually a plus; this means that it's straightforward to extend as part of another system. If you want to stick with your original comparison, you might see it as a sweet spot or a local maximum in the spectrum between inets and yaws when it comes to building interesting Web applications.


To make a Java analogy, mochiweb is like Java EE Servlets for Erlang. It provides a container/servlet programming model similar to Java EE, but with much less overhead.

Nitrogen

参考スライド: Nitrogen Web Framework

Erlang をインストールしたら、次のコマンドを実行して環境変数を設定します。export ERL_LIBS=/usr/lib/erlang/lib (ここに Erlang ライブラリがインストールされていると仮定します) Nitrogen をインストールするには、

  1. Nitrogen をダウンロードまたは チェックアウトした後、解凍して nitrogen ディレクトリを作ります。$ERL_LIBS ディレクトリにコピーします。
  2. $ERL_LIBS/nitrogen ディレクトリに移動して、make コマンドを実行して、Nitrogen をビルドします。
  3. 任意のディレクトリから、nitrogen コマンドを実行して、新しいプロジェクトが作れるようにするには、support ディレクトリの nitrogen という実行ファイルへリンクを作る必要があります。#sudo ln -s $ERL_LIBS/nitrogen/support/nitrogen /usr/local/bin/nitrogen

新しいプロジェクトを作るときには、

  1. 任意のディレクトリから、次のコマンドを実行:nitrogen create sampleweb
  2. sampleweb ディレクトリに入って ./start.sh を実行。
  3. ウェブブラウザ上でhttp://localhost:8000 にアクセスしてみる。

Nitrogenのホームページを参照してください。新しいウェブページの作り方、デフォルトのポート番号の変更、その他いろいろの情報を得ることができます。

脚注

  • プログラムを書くとき、Nitrogen が新しいソースコードを自動的に更新するようにさせたかったら、(シェルからではなく)Erlang のコマンドモードで、sync:go() を実行します。
  • デバッグのためコンソール画面に変数の値を表示するには、DEBUG, LOG または PRINT マクロを使います。(wf.inc 参照)
  • create コマンドで新しくプロジェクトを作るとき(上の例では sampleweb)次のようなディレクトリ構造が作られます。
doc/
ebin/
src/
    pages/
wwwroot/
Emakefile
Makefile
start.sh

nitrogen page /really/helloworld.erl を実行すると、src/pages ディレクトリに really_helloworld.erl というファイルが作られます。sync:go() コマンドを実行すると、Nitrogen は really_helloworld.erl をビルドして、ebin ディレクトリに really_helloworld.beam を生成します。really_world ページの URL は、http://localhost:8000/web/really/helloworld です。

Nitrogen はInet, Yaws, MochiWeb の3種類のウェブサーバ上で実行できます。デフォルトでは、Nitrogen は Inets の上で走ります。Yaws または MochiWeb 上で走らせるには、(1) これら2つをインストール (2) load path を適切に設定 (-pa パラメータを使用。やや複雑) (3) ebin/nitrogen.app を{platform, inets} から {platform, yaws} または {platform, mochiweb} に修正する必要があります。

(原文・未完。続きが来たら翻訳します)
(2009年5月14日 Nitrogen の節を追加)
==翻訳終了