CowboyでHelloWorld!

どうせなら動くものをということで、簡単なWebアプリを作成。

基本のHelloWorldからということで。

 

rebarというツールを全力で使います!

 

適当にアプリケーション用のfolderを作成して、そこにrebarを配置します。

rebarに関することは割愛・・・w

 

まずはプロジェクトのひな形を作ります。

 

./rebar create-app appid=hello_world

 

とすることで、srcフォルダが作成され、その下に

hello_world.app.src

hello_world_app.erl

hello_world_sup.erl

が作成されます。

 

で、今回は軽量WebServerのcowboyを使うので、

rebar.configを作成し、

 

{deps,

  [

   {cowboy,

    ".*", {git, "git@github.com:extend/cowboy.git", {branch, "master"}}}

  ]

}.

 

を追加します。

 

で、

 

./rebar get-deps

 

でdepsフォルダが作成されcowboyを自動で持ってきてくれます。

 

ここからが本題。

 

まずは、hello_world.app.srcを編集。

{application, hello_world,

 [

  {description, "Hello World Application"},

  {vsn, "1"},

  {registered, [hello_world_sup]},

  {applications, [

                  kernel,

                  stdlib,

                  cowboy

                 ]},

  {mod, { hello_world_app, }},

  {env, }

 ]}.

 

な形に修正。

 

次は、hello_world_app.erlを編集。

start(_StartType, _StartArgs) ->

    Dispatch = cowboy_router:compile([

        {'_', [

            {"/", toppage_handler, }

        ]}

    ]),

    {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [

        {env, [{dispatch, Dispatch}]}

    ]),

    hello_world_sup:start_link().

 

これで、port8080で待機するhttpサーバがたつ!

/で受けた物を全てtoppage_handlerに委譲する感じになってます。

 

で、ここでtoppage_handler.erlを作成。

中身はこんな感じ。

-module(toppage_handler).

 

-behaviour(cowboy_http_handler).

 

-export([init/3, handle/2, terminate/3]).

 

init(_Type, Req, ) ->

    {ok, Req, undefined}.

 

handle(Req, State) ->

    {ok, Req2} = cowboy_req:reply(200, [

        {<<"content-type">>, <<"text/plain">>}

    ], <<"Hello World!">>, Req),

    {ok, Req2, State}.

 

terminate(_Reason, _Req, _State) ->

    ok.

 

cowboy_http_handlerの振る舞いをさせたいので、behaviourを指定しています。

これ、どうもDockTyping的な感じっぽいので、init/2 handle/2 terminate/3を実装しておけば、-behaviourの行はなくても問題内っぽい。(消しても動いた。)

ただ、コンパイル時に解るのでつけといた方がよさげ。

 

hello_world_sup.erlは特にいじる必要はなし。

 

ここまでできたら、./rebar compileでコンパイルできることを確認。

問題なければ、ひとまず完成!

 

で、せっかくなのでapplicationとして、生成物を作りましょう。

 

relというフォルダを作成し、relの中で、./rebar create-node nodeid=hello_world

そうすると、いろいろファイルとかが作成されます。

で、作成されたreltool.configを編集。

中身をこんな感じに修正。

{sys, [

       {lib_dirs, ["../deps"]},

       {erts, [{mod_cond, derived}, {app_file, strip}]},

       {app_file, strip},

       {rel, "hello_world", "1",

        [

         kernel,

         stdlib,

         sasl,

         cowboy,

         hello_world

        ]},

       {rel, "start_clean", "",

        [

         kernel,

         stdlib

        ]},

       {boot_rel, "hello_world"},

       {profile, embedded},

       {incl_cond, derived},

       {mod_cond, derived},

       {excl_archive_filters, [".*"]}, %% Do not archive built libs

       {excl_sys_filters, ["^bin/.*", "^erts.*/bin/(dialyzer|typer)",

                           "^erts.*/(doc|info|include|lib|man|src)"]},

       {excl_app_filters, ["\.gitignore"]},

       {app, hello_world, [{mod_cond, app}, {incl_cond, include}, {lib_dir, ".."}]}

      ]}.

 

{target_dir, "hello_world"}.

 

{overlay, [

           {mkdir, "log/sasl"},

           {copy, "files/erl", "\{\{erts_vsn\}\}/bin/erl"},

           {copy, "files/nodetool", "\{\{erts_vsn\}\}/bin/nodetool"},

           {copy, "files/hello_world", "bin/hello_world"},

           {copy, "files/hello_world.cmd", "bin/hello_world.cmd"},

           {copy, "files/start_erl.cmd", "bin/start_erl.cmd"},

           {copy, "files/install_upgrade.escript", "bin/install_upgrade.escript"},

           {copy, "files/sys.config", "releases/\{\{rel_vsn\}\}/sys.config"},

           {copy, "files/vm.args", "releases/\{\{rel_vsn\}\}/vm.args"}

          ]}.

 

で、relから出てapplicationフォルダにて、rebar.configを編集。

{sub_dirs, ["rel"]}.

を追加し保存。

 

で、

./rebar generate

すると、relフォルダの下にhello_worldというフォルダが作成されて、生成物がいろいろできます。

 

これで、リリース品が完成。

 

起動は

./rel/hello_world/bin/hello_world console

で、起動できます。

 

特にエラーが出なければ、起動完了。

 

127.0.0.1:8080にアクセスするとHello World!が表示されます。

 

かなりはしょりましたが、これでCowboyでHello World!完結ということでw