This is the process of setting up erlang, rebar3, and cowboy for a Hello World, starting with a clean Debian 8 install.
Update apt and install deps:
root@046edcaea45a:~# apt-get update
root@046edcaea45a:~# apt-get install erlang erlang-dev gcc
root@046edcaea45a:~# wget https://s3.amazonaws.com/rebar3/rebar3
root@046edcaea45a:~# mkdir ~/bin/
root@046edcaea45a:~# mv rebar3 ~/bin/
root@046edcaea45a:~# chmod +x ~/bin/rebar3
Create the rebar project:
root@046edcaea45a:~# ~/bin/rebar3 new release cowboy_hello_world
root@046edcaea45a:~# cd cowboy_hello_world/
Put the following entry in the rebar.config
deps
tuple and add the rebar3_run plugin
:
root@046edcaea45a:~/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]}]}
root@046edcaea45a:~/cowboy_hello_world# vi apps/cowboy_hello_world/src/cowboy_hello_world.app.src
Register a handler for the /
url
root@046edcaea45a:~/cowboy_hello_world# 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}.
Create the handler referenced above:
root@046edcaea45a:~/cowboy_hello_world# 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}.
root@046edcaea45a:~/cowboy_hello_world# ~/bin/rebar3 run
Browse to http://localhost:2938 and win.
If you want to try this with Cowboy 2.0.0-rc.1, currently the latest preview release of Cowboy 2.x, you'll need to make a few changes. Since version 2.0.0-pre.1 in many places in the API property list arguments (
[{key1, value1}, ..., {keyN, valueN}]
) have been replaced with maps (#{key1 => value1, ..., keyN => valueN}
), and cowboy:start_http/4 has been replaced with cowboy:start_clear/3. Update the Cowboy version inrebar.config
, runrebar3 upgrade
, then apply the following change to your files.apps/cowboy_hello_world/src/cowboy_hello_world_app.erl
apps/cowboy_hello_world/src/cowboy_hello_world_root.erl