2017년 6월 21일 수요일

Erlang - Getting Started using rebar3,cowboy in MacOSX


brew doctor

brew update

brew upgrade

brew install openssl

curl -O https://raw.githubusercontent.com/kerl/kerl/master/kerl && chmod a+x kerl

export KERL_BUILD_BACKEND=git

export OTP_GITHUB_URL="https://github.com/basho/otp"

kerl list releases

kerl update releases

KERL_CONFIGURE_OPTIONS="--disable-hipe --enable-smp-support --enable-threads
                        --enable-kernel-poll --with-ssl=/usr/local/opt/openssl --without-odbc --enable-darwin-64bit" kerl build 19.2 19.2

kerl install 19.2 ~/erlang/19.2

. ~/erlang/19.2/activate

erl -version

wget https://s3.amazonaws.com/rebar3/rebar3 && chmod +x rebar3

./rebar3 local install

rebar3 new release cowboy_hello_world 

cd cowboy_hello_world/

vi rebar.config 

{deps, [
        {cowboy, {git, "https://github.com/ninenines/cowboy", {tag, "2.0.0-pre.1"}}}
]}.

{plugins, [rebar3_run]}.


Add cowboy to the list {application, [{applications, [kernel, stdlib, cowboy]}]}

 vi apps/cowboy_hello_world/src/cowboy_hello_world.app.src 

vi apps/cowboy_hello_world/src/cowboy_hello_world_app.erl 

start(_StartType, _StartArgs) ->
    {ok, Pid} = 'cowboy_hello_world_sup':start_link(),
    Routes = [ {
        '_',
        [
            {"/", cowboy_hello_world_root, []}
        ]
    } ],
    Dispatch = cowboy_router:compile(Routes),

    NumAcceptors = 10,
    TransOpts = [ {ip, {0,0,0,0}}, {port, 2938} ],
    ProtoOpts = [{env, [{dispatch, Dispatch}]}],

    {ok, _} = cowboy:start_http(chicken_poo_poo,
        NumAcceptors, TransOpts, ProtoOpts),

    {ok, Pid}.

vi apps/cowboy_hello_world/src/cowboy_hello_world_root.erl

-module(cowboy_hello_world_root).

-export([init/2]).

init(Req, Opts) ->
    Req2 = cowboy_req:reply(200,
        [{<<"content-type">>, <<"text/plain">>}],
        <<"Hello Erlang!">>,
        Req),
    {ok, Req2, Opts}.

rebar3 run

curl http://localhost:2938