LoginSignup
15
15

More than 5 years have passed since last update.

erlang + rebar3 + cowboyの最小構成

Last updated at Posted at 2016-06-22

並列分散処理を得意とするerlangという関数型言語に興味のある方は多いではないでしょうか?
自分は身近にerlang識者がいたため、興味を持つ事になりました。
今回は、そんなerlangの入門として 「erlang + rebar3 + cowboy」 でHTTPサーバを構築したいと思います。

erlangについて

並列分散処理に強い関数型言語。
OTP(Open Telecom Platform)フレームワークがすばらしい。

参考書の受け売りなので、ニュアンスが間違っているかもですが、大体こんな感じで書いてました。

rebar3について

Erlangアプリケーションのコンパイル等一括で行ってくれる便利なツール。
また、erlangモジュール等のdependencies(依存関係)を一括管理してくれたりもしています。
rebar3より以前にrebarが存在しましたが2016年6月現在は非推奨となっており、rebar3の利用を勧められます。

cowboyについて

Erlangで書かれた、小型かつ高速なHTTPサーバモジュール。

環境とIDE

  • mac osx 10.9.5
  • Erlang/OTP 17.5
  • eclipse neon + erl ide

rebar3インストール

rebar3のインストールはhomebrewを利用します。

$ brew install homebrew/versions/rebar3

テンプレート作成

rebar3を利用してerl_baseテンプレートを作成します。

$ rebar3 new app erl_base

フォルダ構成

erl_base(workfolder)
├─LICENSE
├─README.md
├─rebar.config
└─src
  └─erl_base
    ├─erl_base.app.src
    ├─erl_base_app.erl
    └─erl_base_sup.erl 

deps(依存関係)設定

rebar.configのdepsにcowboyを追加します。
今回はcowboyだけ使うのでこれだけです。

rebar.config
{erl_opts, [debug_info]}.
{deps, [
  {cowboy, ".*", {git, "git://github.com/ninenines/cowboy", {tag, "2.0.0-pre.3"}}}
]}.

erl_base.app.srcのapplicationsにcowboyを追加します。

src/erl_base.app.src
………
  {applications,
   [kernel,
    stdlib,
    cowboy
   ]},
………

httpサーバ処理部分実装

erl_base.erlでは各アプリケーションを順番にstartさせています。

src/erl_base.erl
-module(erl_base).

-export([start/0]).

start() ->
    ok = application:start(crypto),
    ok = application:start(ranch),
    ok = application:start(cowlib),
    ok = application:start(cowboy),
    ok = application:start(erl_base).

erl_base_handler.erlファイルを作成します。
ここでは単純なjsonデータを返すような処理を実装しています。

src/erl_base_handler.erl
-module(erl_base_handler).

-export([init/2]).

init(Req, Opts) ->
  Req2 = cowboy_req:reply(200, [
    {<<"content-type">>, <<"application/json">>}
  ], <<"{'message': 'Hello world!'}">>, Req),
  {ok, Req2, Opts}.

最後にerl_base_app.erlにcowboy:start_httpを設置してhttpサーバ立ち上げ処理を実装します。
先ほど作成したerl_base_handlerをここで設定して http://127.0.0.1/にアクセスが来た時に処理を行うように対応しました。

src/erl_base_app.erl
………
start(_StartType, _StartArgs) ->
    Dispatch = cowboy_router:compile([
      {'_', [
        {"/", erl_base_handler, []}
      ]}
    ]),
    {ok, _} = cowboy:start_http(http, 100, [{port, 8010}], [
      {env, [{dispatch, Dispatch}]}
    ]),
    erl_base_sup:start_link().

………

rebarでupgradeとcompile

depsに設定したcowboy等をupgradeしてcompileします

$ rebar3 upgrade
$ rebar3 compile

サーバ起動

サーバを起動させます。

$ erl -pa ebin _build/default/lib/*/ebin -s erl_base -noshell

erlangが実行されたら
http://localhost:8010/
をブラウザーで確認してみると下図のようなページが表示されます。

スクリーンショット 2016-06-21 17.01.08.png

また、サーバ起動をdaemon化させたい場合は以下のコマンドで実現出来ます。

$ erl -pa ebin _build/default/lib/*/ebin -s erl_base -noshell -detached

github

上記対応を行ったソースコードをgithubにあげているので、興味がある方はcloneしてみて下さい。
https://github.com/yu-sa/erl_base

まとめ

  • すごく単純なページならすぐ立ち上げられるようです。
  • まだ、並列処理の部分は全く触っていないので、ここからスタートして色々試して見たいです。
15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15