LoginSignup
2
2

More than 5 years have passed since last update.

RebarとCowboyでindex.htmlファイルを表示

Last updated at Posted at 2014-12-30

erlang.mkを使ってリリース作成に関しては次の投稿を参照してください。ninenines.euオフィシャルサイトはerlang.mkビルドシステムを使っていますが,個人的にはRebarにはまたなれていなくて,とりあえずRebarでプロジェクトをリリースしたいので,Rebarでcowboyのプロジェクトをビルドしてみました.Rebarのこと分かった後に,erlang.mkに移るかもしれません.

hello_worldプロジェクトを作成します.

$ rebar create-app appid=hello_world
==> hello_world (create-app)
Writing src/hello_world.app.src
Writing src/hello_world_app.erl
Writing src/hello_world_sup.erl

rebar.configを作成します.sub_dirsはリリースを作成するために入れてあります.

{ sub_dirs, ["rel"] }.

{deps, [
        {cowboy, ".*", {git, "https://github.com/extend/cowboy.git"}}
       ]
}.

そして依頼するパッケージcowboyをダウンロードします.
$ rebar get-deps

下はsrc/hello_world_app.erlファイル

-module(hello_world_app).
-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

start(_StartType, _StartArgs) ->
    Dispatch = cowboy_router:compile([
        {'_',
              [
                {"/", cowboy_static, {priv_file, hello_world, "index.html"}}
              ]
        }
    ]),
    {ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
                                [
                                    {env, [{dispatch, Dispatch} ] }
                                ]),
    hello_world_sup:start_link().

stop(_State) ->
    ok.

src/hello_world.app.srcのapplications部分はcowboyを追加します.

$ cat hello_world.app.src
{application, hello_world,
 [
  {description, ""},
  {vsn, "1"},
  {registered, []},
  {applications, [
                  kernel,
                  stdlib,
                  cowboy
                 ]},
  {mod, { hello_world_app, []}},
  {env, []}
 ]}.

privディレクトリを作成します.priv/index.htmlも用意します

$ mkdir priv
$ cat index.html
<h1>Hello world from cowboy</h1>

これからリリースを作成します.

$ mkdir rel
$ cd rel
$ rebar create-node nodeid=hello_world
==> rel (create-node)
Writing reltool.config
Writing files/erl
Writing files/nodetool
Writing files/hello_world
Writing files/sys.config
Writing files/vm.args
Writing files/hello_world.cmd
Writing files/start_erl.cmd
Writing files/install_upgrade.escript

reltool.configファイルを編集します.

{lib_dirs, []},

{lib_dirs, ["../deps", "../apps"]},
に変更します.

apps/hello_worldディレクトリも必要
$ mkdir -p apps/hello_world

ebin/ディレクトリを用意します.

$ mkdir ebin

シンボリックリンクをapps/hello_world/の下に作成(リリース作成するときにapp/hello_world下にソースコードが必要みたいなので、作成しました)

mkdir -p apps/hello_world
cd apps/hello_world
ln -s ../../src
ln -s ../../ebin
ln -s ../../priv
cd ../..

リリースを作成します

$ rebar compile generate
==> cowlib (compile)
==> ranch (compile)
==> cowboy (compile)
==> rel (compile)
==> hello_world (compile)
Compiled src/hello_world_sup.erl
Compiled src/hello_world_app.erl
==> rel (generate)
WARN:  'generate' command does not apply to directory /Users/wzj/Projects/Web-Programming/Erlang/hello_world

アプリケーションを起動します.

$ ./rel/hello_world/bin/hello_world console

これで127.0.0.1:8080にアクセスするとindex.htmlファイルが表示されます.

ディレクトリ構造

$ tree -L 2
.
├── apps
│   └── hello_world
├── deps
│   ├── cowboy
│   ├── cowlib
│   └── ranch
├── ebin
│   ├── hello_world.app
│   ├── hello_world_app.beam
│   └── hello_world_sup.beam
├── priv
│   └── index.html
├── rebar.config
├── rel
│   ├── files
│   ├── hello_world
│   └── reltool.config
└── src
    ├── hello_world.app.src
    ├── hello_world_app.erl
    └── hello_world_sup.erl

12 directories, 9 files
2
2
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
2
2